Help me in collecting the most common 'gotchas' that a new user of powershell would hit.

link|improve this question
When I tried PowerShell, it seemed like a pit-fall in itself. I stuck to Cygwin shell since it was available. Probably something to do with the bash shell being around since before. – nik Jul 19 '09 at 10:40
7  
@nik - more likely something to do with you not being bothered to RTFM. – x0n Jul 19 '09 at 20:52
I'd never realised before, but Powershell's syntax looks like C#.net and Perl gangbanged Ruby in a dark alley. – Tom O'Connor Aug 14 '11 at 9:39
feedback

closed as not a real question by Chris S, MikeyB, Ben Pilbrow, jscott, Iain Aug 11 '11 at 16:41

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. See the FAQ for guidance on how to improve it.

5 Answers

When dealing with XML that has an item element (like an RSS feed), the name of the element conflicts with the Item indexer property on the XmlNode class e.g.:

PS> $url = 'http://blogs.msdn.com/powershell/rss.xml'
PS> $feed = [xml](new-object System.Net.WebClient).DownloadString($url)
PS> $feed.rss.channel
format-default : The member "Item" is already present.
    + CategoryInfo : NotSpecified: (:) [format-default], ...
    ...

One way to work around this is to replace Item with Item2 in the string version of the XML before converting it to [xml], like so:

PS> $feed = (new-object System.Net.WebClient).DownloadString($url)
PS> $feed2 = [xml]($feed -replace 'Item>','Item2>')
PS> $feed2.rss.channel

title       : Windows PowerShell Blog
link        : http://blogs.msdn.com/powershell/default.aspx
description : Automating the world one-liner at a time.
              <table border=0 width=100%><tr><td align=right>
              <a href='http://blogs.msdn.com/powershell/pages/download-w
              indows-powershell.aspx'><u>Click Here</u></a> to Download  
              PowerShell</td></tr></table>
language    : en-US
generator   : CommunityServer 2.1 SP1 (Build: 61025.2)
Item2       : {Item2, Item2, Item2, Item2...}

Also, keep in mind that if you don't need to look at channel directly, you can use the PowerShell XML adapter to access the item element like so:

PS> $feed.rss.channel.item | foreach {$_.title}
Free PowerShell V1 Book From the Makers of PowerShell Plus
Final Approved Verb List for Windows PowerShell 2.0
Evolution vs. Intelligent Design
Advanced Debugging in PowerShell
Function Help Cannot Share a Cmdlet Help File
PowerScripting Podcast update
Assigning the Output of a SWITCH Statement
...

It seems that it is the output formatter that has the problem with 'Item' XML element names.

link|improve this answer
I think this may be fixed in v2 - I have consumed rss as xml just fine without needing to replace any text. I'll have to verify when I get home. – Goyuix Jul 21 '09 at 16:36
feedback

I think one big pitfall is understanding that everything is moved around in pipes (not unusual) - the difference being the pipe always carries objects, not just a byte stream.

For example, the traditional Hello World program in PowerShell is simply:

"Hello, World!"

As that create a string object, and passes it down the pipe - which ultimately gets evaluated (in this case, .toString() is called as it is displayed to the end user). Once that concept has had time to gel, crazy things like this start making sense:

"Hello, World!" | % { $_.Length }
link|improve this answer
feedback

Not setting the "execution policy" I always start with Set-executionPolicy "unrestricted" on clean machines.

... and YES - I do understand the security risks...

.. KJ

link|improve this answer
3  
I usually set it to RemoteSigned, which works as well unless you store scripts on network shares. – mihi Jul 21 '09 at 17:02
feedback

There are some tricks to running command line utilities that were not built with Powershell in mind:

  • Running an executable that starts with a number in the name, preface it with an Ampersand (&).

    &7zip.exe

  • Wrap each token the utility is expecting to see from the command line in quotes.

    &"c:\path with space\SomeCommand.exe" "/parameter2" "/parameter2" "parameter2's Value" "Value2 `" with a quote"

link|improve this answer
feedback