26

A while back I was reading about multi-variable assignments in PowerShell. This lets you do things like this

64 >  $a,$b,$c,$d = "A four word string".split()

65 >  $a
A

66 >  $b
four

Or you can swap variables in a single statement

$a,$b = $b,$a

What little known nuggets of PowerShell have you come across that you think may not be as well known as they should be?

2
  • Andy, are you restricting this to PowerShell 1.0, or is 2.0 ok to discuss? May 21, 2009 at 14:53
  • 1
    As people post ideas, it would be helpful to say which version (1/2) they are supported in.
    – Nate
    May 21, 2009 at 15:21

11 Answers 11

19

The $$ command. I often have to do repeated operations on the same file path. For instance check out a file and then open it up in VIM. The $$ feature makes this trivial

PS> tf edit some\really\long\file\path.cpp
PS> gvim $$

It's short and simple but it saves a lot of time.

1
  • THere are a lot of great answers and I thank everyone who posted something up here. I chose this one for a couple reasons. I personally did not know about $$ and also it was the first answer. Thanks again to everyone who chimed in. Lots of great input! Jun 12, 2009 at 20:17
19

By far the most powerful feature of PowerShell is its ScriptBlock support. The fact that you can so concisely pass around what are effectively anonymous methods without any type constraints are about as powerful as C++ function pointers and as easy as C# or F# lambdas.

I mean how cool is it that using ScriptBlocks you can implement a "using" statement (which PowerShell doesn't have inherently). Or, pre-v2 you could even implement try-catch-finally.

function Using([Object]$Resource,[ScriptBlock]$Script) {
    try {
        &$Script
    }
    finally {
        if ($Resource -is [IDisposable]) { $Resource.Dispose() }
    }
}

Using ($File = [IO.File]::CreateText("$PWD\blah.txt")) {
   $File.WriteLine(...)
}

How cool is that!

0
15

A feature that I find is often overlooked is the ability to pass a file to a switch statement.

Switch will iterate through the lines and match against strings (or regular expressions with the -regex parameter), content of variables, numbers, or the line can be passed into an expression to be evaluated as $true or $false

switch -file 'C:\test.txt' 
{   
  'sometext' {Do-Something}   
  $pwd {Do-SomethingElse}  
  42 {Write-Host "That's the answer."}  
  {Test-Path $_} {Do-AThirdThing}  
  default {'Nothing else matched'} 
}
2
  • 1
    I think Bruce's book pointed out that switch was one of the most powerful language constructs in PowerShell. Passing in a file is very cool. Thanks Steve. May 22, 2009 at 13:26
  • That he did.. and I find the file part the most overlooked. Thanks for the question! May 23, 2009 at 3:01
13

$OFS - output field separator. A handy way to specify how array elements are separated when rendered to a string:

PS> $OFS = ', '
PS> "$(1..5)"
1, 2, 3, 4, 5
PS> $OFS = ';'
PS> "$(1..5)"
1;2;3;4;5
PS> $OFS = $null # set back to default
PS> "$(1..5)"
1 2 3 4 5

Always guaranteeing you get an array result. Consider this code:

PS> $files = dir *.iMayNotExist
PS> $files.length

$files in this case may be $null, a scalar value or an array of values. $files.length isn't going to give you the number of files found for $null or for a single file. In the single file case, you will get the file's size!! Whenever I'm not sure how much data I'll get back I always enclose the command in an array subexpression like so:

PS> $files = @(dir *.iMayNotExist)
PS> $files.length # always returns number of files in array

Then $files will always be an array. It may be empty or have only a single element in it but it will be an array. This makes reasoning with the result much simpler.

Array covariance support:

PS> $arr = '127.0.0.1','192.168.1.100','192.168.1.101'
PS> $ips = [system.net.ipaddress[]]$arr
PS> $ips | ft IPAddressToString, AddressFamily -auto

IPAddressToString AddressFamily
----------------- -------------
127.0.0.1          InterNetwork
192.168.1.100      InterNetwork
192.168.1.101      InterNetwork

Comparing arrays using Compare-Object:

PS> $preamble = [System.Text.Encoding]::UTF8.GetPreamble()
PS> $preamble | foreach {"0x{0:X2}" -f $_}
0xEF
0xBB
0xBF
PS> $fileHeader = Get-Content Utf8File.txt -Enc byte -Total 3
PS> $fileheader | foreach {"0x{0:X2}" -f $_}
0xEF
0xBB
0xBF
PS> @(Compare-Object $preamble $fileHeader -sync 0).Length -eq 0
True

Fore more stuff like this, check out my free eBook - Effective PowerShell.

10

Along the lines of multi-variable assignments.

$list = 1,2,3,4

While($list) {
$head, $list = $list
$head
}

1
2
3
4

1
  • Here is a new an improved version: while($head, $list = $list) { $head } -- It is a nice array shifting technique
    – Doug Finke
    Apr 26, 2011 at 0:16
8

I've been using this:

if (!$?) {  # if previous command was not successful
    Do some stuff
}

and I also use $_ (current pipeline object) quite a bit, but these might be more known than other stuff.

6

The fact that many operators work on arrays as well and return the elements where a comparison is true or operate on each element of the array independently:

1..1000 -lt 800 -gt 400 -like "?[5-9]0" -replace 0 -as "int[]" -as "char[]" -notmatch "\d"

This is faster than Where-Object.

3

Not a language feature but super helpful

f8 -- Takes the text you have put in already and searches for a command that starts with that text.

3

Tab-search through your history with #

Example:

PS> Get-Process explorer
PS> "Ford Explorer"
PS> "Magellan" | Add-Content "great explorers.txt"
PS> type "great explorers.txt"
PS> #expl <-- Hit the <tab> key to cycle through history entries that have the term "expl"

2

Love this thread. I could list a ton of things after reading Windows Powershell in Action. There's a disconnect between that book and the documentation. I actually tried to list them all somewhere else here, but got put on hold for "not being a question".

I'll start with foreach with three script blocks (begin/process/end):

Get-ChildItem | ForEach-Object {$sum=0} {$sum++} {$sum}

Speaking of swapping two variables, here's swapping two files:

${c:file1.txt},${c:file2.txt} = ${c:file2.txt},${c:file1.txt}

Search and replace a file:

${c:file.txt} = ${c:file.txt} -replace 'oldstring','newstring'

Using assembly and using namespace statements:

using assembly System.Windows.Forms
using namespace System.Windows.Forms
[messagebox]::show('hello world')

A shorter version of foreach, with properties and methods

ps | foreach name
'hi.there' | Foreach Split .

Use $() operator outside of strings to combine two statements:

$( echo hi; echo there ) | measure

Get-content/Set-content with variables:

$a = ''
get-content variable:a | set-content -value there

Anonymous functions:

1..5 | & {process{$_ * 2}}

Give the anonymous function a name:

$function:timestwo = {process{$_ * 2}}

Anonymous function with parameters:

& {param($x,$y) $x+$y} 2 5

You can stream from foreach () with these, where normally you can't:

& { foreach ($i in 1..10) {$i; sleep 1} } | out-gridview

Run processes in background like unix '&', and then wait for them:

$a = start-process -NoNewWindow powershell {timeout 10; 'done a'} -PassThru
$b = start-process -NoNewWindow powershell {timeout 10; 'done b'} -PassThru
$c = start-process -NoNewWindow powershell {timeout 10; 'done c'} -PassThru
$a,$b,$c | wait-process

Or foreach -parallel in workflows:

workflow work {
  foreach -parallel ($i in 1..3) { 
    sleep 5 
    "$i done" 
  }
}

work

Or a workflow parallel block where you can run different things:

function sleepfor($time) { sleep $time; "sleepfor $time done"}

workflow work {
  parallel {
    sleepfor 3
    sleepfor 2
    sleepfor 1
  }
  'hi'
}

work 

Three parallel commands in three more runspaces with the api:

$a =  [PowerShell]::Create().AddScript{sleep 5;'a done'}
$b =  [PowerShell]::Create().AddScript{sleep 5;'b done'}
$c =  [PowerShell]::Create().AddScript{sleep 5;'c done'}
$r1,$r2,$r3 = ($a,$b,$c).begininvoke()

$a.EndInvoke($r1); $b.EndInvoke($r2); $c.EndInvoke($r3) # wait
($a,$b,$c).Streams.Error # check for errors
($a,$b,$c).dispose() # cleanup

Parallel processes with invoke-command, but you have to be at an elevated prompt with remote powershell working:

invoke-command localhost,localhost,localhost { sleep 5; 'hi' } 

An assignment is an expression:

if ($a = 1) { $a } 
$a = $b = 2

Get last array element with -1:

(1,2,3)[-1]

Discard output with [void]:

[void] (echo discard me)

Switch on arrays and $_ on either side:

switch(1,2,3,4,5,6) {
  {$_ % 2} {"Odd $_"; continue}
  4 {'FOUR'}
  default {"Even $_"}
}

Get and set variables in a module:

'$script:count = 0
$script:increment = 1
function Get-Count { return $script:count += $increment }' > counter.psm1 # creating file

import-module .\counter.psm1

$m = get-module counter
& $m Get-Variable count
& $m Set-Variable count 33

See module function definition:

& $m Get-Item function:Get-Count | foreach definition

Run a command with a commandinfo object and the call operator:

$d = get-command get-date
& $d

Dynamic modules:

$m = New-Module {
  function foo {"In foo x is $x"}
  $x=2
  Export-ModuleMember -func foo -var x
}

flags enum:

[flags()] enum bits {one = 1; two = 2; three = 4; four = 8; five = 16}
[bits]31

Little known codes for the -replace operator:

$number Substitutes the last submatch matched by group number.
${name} Substitutes the last submatch matched by a named capture of the form (?).
$$ Substitutes a single "$" literal.
$& Substitutes a copy of the entire match itself.
$` Substitutes all the text from the argument string before the matching portion.
$' Substitutes all the text of the argument string after the matching portion.
$+ Substitutes the last submatch captured.
$_ Substitutes the entire argument string.

Demo of workflows surviving interruptions using checkpoints. Kill the window or reboot. Then start PS again. Use get-job and resume-job to resume the job.

workflow test1 {
  foreach ($b in 1..1000) {
    $b
    Checkpoint-Workflow
  }
}
test1 -AsJob -JobName bootjob

Emacs edit mode. Pressing tab completion lists all the options at once. Very useful.

Set-PSReadLineOption -EditMode Emacs

Any command that begins with "get-", you can leave off the "get-":

date
help

End parsing --% and end of parameters -- operators.

write-output --% -inputobject
write-output -- -inputobject

Tab completion on wildcards:

cd \pro*iles # press tab

Compile and import a C# module with a cmdlet inside, even in Osx:

Add-Type -Path ExampleModule.cs -OutputAssembly ExampleModule.dll
Import-Module ./ExampleModule.dll
1

Iterate backwards over a sequence just use the len of the sequence with a 1 on the other side of the range:

foreach( x in seq.length..1) { Do-Something seq[x] }

Not the answer you're looking for? Browse other questions tagged or ask your own question.