Ashley Barrell

PowerShell Tidbits

I’ve been working with PowerShell for a while now and there are a lot of features and gotchyas that people aren’t really aware of ,so I thought it would be a good idea to get some of these things written down and out there so people can know about them.

PowerShell Features

List Suggestions

If you press Ctrl + Space you can get suggestions in a browsable format. This works for listing files in a directory, arguments to a cmdlet or script, and cmdlets themselves. Try pressing Ctrl + Space in different places and see what PowerShell can suggest!

Suggestion List

If you press Ctrl + R you can do a backwards search through your command history. This is especially useful if you know you’ve entered a particular command but can’t exactly remember it. You don’t have to limit yourself to only a command. If you start typing anything the search will try and match what it can through your history. This means you can start typing an argument or a value to an argument and it will show you the entire command that you ran. If you keep pressing Ctrl + R when you have a match it’ll scroll through all the matches you have.

Backwards Search

Language Features

Function Return Types

The following function does not always return an array:

1
2
3
4
5
6
7
8
9
10
11
function My-Function
{
    [OutputType([Array])]
    param(
        [string[]]$MyParam
    )
    
    # Do something...
    
    return $MyParam
}

If you try and return an array from a function but the array contains 1 element, PowerShell will automatically convert the type into a single object rather than an object array. This is rather annoying default behaviour of PowerShell and even though we are explicitly specifying that we are returning an array, PowerShell will ignore the output type and convert it anyway. To handle cases like these make sure you are capturing the return value into a variable and setting the type of that variable as an array:

1
[Array]$MyVar = (My-Function 'foo')

Dynamic Arrays

By default arrays in PowerShell are of a fixed size. You can resize them and append elements using the += operator but this is a rather slow operation with arrays. If you need a dynamically resizing array you can use the dotnet array list:

1
2
[System.Collections.ArrayList]$myArray = @()
$myArray.Add(1)