Hidden Features of PowerShell
What are the hidden features of PowerShell?
Force Powershell functions to really return an array, even an empty array.
Due to the way the @() syntax is implemented, functions may not always return an array as expected, e.g. the following code will return a $null and NOT an empty array. If you are testing code with set-StrictMode -On
set, you'll get an PropertyNotFoundStrict
error instead when trying to reference the .count
property:
function test
{
#some code that might return none,one or multiple values
$data = $null
return @($data)
}
(test).count
Simply prepending a ,
to the @()
will bypass the "syntactic sugar" and you'll have an actual array returned, even if it's empty:
function test2
{
#some code that might return none,one or multiple values
$data = $null
return ,@($data)
}
(test2).count
Make your own custom functions and save them in your profile. You can build tons of useful functions without having to re-think it all every time a similar problem pops up.
Edit your profile:
PS C:\> notepad $profile
Access any .net classes by using Add-Type -Assembly
with the assembly name, or Add-Type -Path
with the dll path, and then using syntax like [Namespace.Dotted.ClassName+NestedClass]::StaticMethod()
to create a New-Object Namespace.Dotted.ClassName
or invoke static methods/fields on types.
Save files using different encoding than UTF-16 in ISE.
The Powershell ISE defaults to saving all files as "Unicode Big Endian" (UTF-16) encoding. The following code will create a menu item in the ISE and assign a hotkey (default, Ctrl-Shift+E) to save the file in the current PowerShellISE tab using a specified encoding other than UTF-16. I set it to UTF-8, but you could use ASCII or something else if desired.
$iseProfile = $profile -replace '_profile','ISE_profile'
$addMenuCmdStr = '$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add("_Save as UTF8",{$psIse.CurrentFile.Save([System.Text.Encoding]::UTF8)},"Ctrl+Shift+E")'
add-content $iseProfile -value $addMenuCmdStr
ise
This trick is useful for avoiding certain issues with PowerShell scripts such as:
Subversion (possibly other CVS systems) adds .ps1 files to the repository as binary and not as plain text, won't allow you to "diff" your scripts because they are "binary", or generates an error that a file cannot be added because it is "binary mime-type". Changing the encoding to UTF-8 should allow your files to be added to the repository with svn:mime-type/text-plain and allows the diff functions to work.
Signing code using set-AuthenticodeSignature fails for some files and not others
Probably other cases were file content operations work for some files and not others, often for no apparent reason, but one symptom is that only files created with the ISE have the issue.