Getting Started with PowerShell
Why are so many admins afraid of little old PowerShell? Probably because they don't understand it. Don't let common misconceptions stand in your way -- get to know the power of PowerShell and increase your value in the workforce.
Q. Do normal Windows commands run in PowerShell?
A. Generally, yes. There are times, however, when PowerShell's parsing of commands results in unexpected and confusing errors. Sometimes, you'll need to enclose your entire command string within quotation marks and use the shell's invoke operator to run the command:
&"SC.EXE STOP WUAUSERV"
That's a good example because it uses the external Sc.exe command rather than PowerShell's internal SC alias. If you just run
Debugging in Windows PowerShell
Bugs are an inevitable part of life when you write PowerShell scripts. Here are some basic techniques for hunting down and squashing bugs and some practices that can help keep bugs at bay.
Q. Are 64-bit and 32-bit PowerShell the same?
A. Nope. For the most part, you won't encounter any differences, but each version of the shell can only load matching snap-ins in some cases, meaning you'll have to be careful to download the correct 64- or 32-bit edition of any add-ons you want to use. Apart from add-on compatibility, there aren't any major functional differences between the two versions.
Q. Can PowerShell read and parse XML files?
A. Yes, although the means isn't very obvious. If you have an XML file named Mine.xml, do this:
\\[xml\\]$xml = Get-Content Mine.xml
That'll read the file, parse the XML, and construct within the $xml variable an object tree based on the XML structure. Run
Q. How can I map a network drive in PowerShell?
A. To use strictly within PowerShell, use the New-PSDrive cmdlet. The resulting drive won't be visible in Explorer. If you want to map a more traditional systemwide network drive, use good ol' NET USE, just like you would have done in Cmd.exe.
Q. How do I use Test-Connection in PowerShell?
A. This cmdlet basically uses WMI's Win32_PingStatus under the hood. It returns, by default, four "result" objects, each of which contains various properties, including a StatusCode, which will be 0 for successful pings. Sometimes, you just want to know if it worked or not, without all that extra information. In those cases, use:
Test-Connection remotehost -quiet
And you'll get back a simple True or False, which can be used in an If construct:
Q. Why can't I pipe a formatted table to a CSV file?
A. You can, but the CSV file will contain the output of the Format-Table cmdlet, which probably isn't what you want. Format cmdlets produce objects that tell the shell how to construct an on-screen display, which is why this produces such ugly output, as this example shows: