- The script parser reads from top to bottom, and if it hasn't read a function before it's called, it won't find the function. Yeah, I know. Just like all of the quirks that I'll list here, I'm sure they had a good reason for making it work like that, but it was painful learning it. So you'll just need to put all of your functions at the top, and the part of the script that runs first is at the end of the file.
- Passing multiple parameters to a function requires no parentheses, and no commas. Yeah, so when you call a function that has more than one parameter, don't use parentheses, and delimit the parameters with spaces.
foo param1 param2 parm3
- Update PowerShell before you start coding. PowerShell is not updated by Windows Update, so you'll want to get the latest before you start hacking away.
- Passing arrays as parameters doesn't work unless you do it "right."
[string[]]$recipients = "me@outlook.com", "me@yahoo.com"
will of course create your array (and there are several other ways to remove the dermal tissue from that feline), andfunction foo([string[]] $TO)
will create the function's array. Then within your function, just don't use quotes around $TO when you use it. See this. - If the contents of a variable need to contain a dollar sign, use the back tick (the single quote on the tilde key).
$MyDollarSignContainingVariable = WeirdService`$Name
So this will returnWeirdService$Name
- In the PowerShell ISE, you have be on a line that contains code to set a breakpoint. I feel kind of stupid because I can see that it makes sense, but sometimes you just think, "Oh, it's broken," and stop trying because you have another way of doing it (adding write-host messages) that you know works.
- Functions return all output, not just what you specify after the return statement. So if you have the following lines in a function:
$t = "whatever"
The function will actually return an array containing "True" and "what". Yeah, no comment. So within functions, you have to pipe things to out-null if you don't want them to return anything.
$t -match "what"
return $matches$t = "whatever"
This will just return "what" as one might have hoped for in the first place.
$t -match "what" | out-null
return $matches - There is no "pause" statement. Ah, it's one of the most used DOS batch statements, and for some odd reason it hasn't been included as a Powershell statement.
So there you have it--my PowerShell soul laid bare for all to see. Oh, and there is now an extension for Visual Studio for PowerShell, but I haven't done much with it yet. Use NuGet in VS to find it. Hoping it will be nicer than the ISE.
I'm sure that these little quirks, and many others, have caused many to ditch PowerShell and go back to a batch or VBScript. It takes a lot of time to work through all of these little quirks, and many just don't feel like they have the time. If you know of others, post a comment--it would be nice to have a good list of "gotchas" for PowerShell newbies as it would save a generation of scripters a lot of time.
Comments