PowerShell Tricks

1. Array Parameter

Suppose a function has a parameter, accepting array type.

Get-Process -Name <string[]>
Get-Process -Name @("powershell", "explorer")
Get-Process -Name powershell, explorer

2. Array

If a function return more than one variables, the result would be array.

function Get-Something {
return 'Hello', 'World'
}
(Get-Something).GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
$range = 1, 2, 3, 4, 5
$range[2..4]
3
4
5
("azz", "abz", "zzz").trim("z")
a
ab

3. Splatting

If a function have a couple of parameters that cannot display in single line. you have two options:

  • Wrap the editor
  • Use line breaker
function Get-Process {
param (
[string[]]Name,
[int[]]Id
)
}
$process = @{
Name = 'explorer'
}
Get-Process @process

4. Property

For a custom object, it can add custom property dynamically. How retrieve the property value if don’t know the property name exactly. PowerShell’s . operation can do that in the runtime.

$object = [pscutomobject]@{"some name" = "value"}
$object."some name" # value
$object.{some name} # value
$prop = "some name"
$object.$prop # value

5. Select-Object

Select-Object is widely used command in PowerShell daily work. It can achieve couples of goals via parameter combination.

  • Select properties: Get-Process | Select-Object -Property Name, Id
  • Select properties with wildcards: Get-Process | Select-Object -Property Name, *Memory
  • Select properties but exclude ones: Get-Process | Select-Object -Property * -Exclude Memory
  • Get first objects: Get-Process | Select-Object -First 2
  • Get last objects: Get-Process | select-Object -Last 2
  • Get object but skip ones: Get-Process | Select-Object -Skip 4 -First 1
  • Select property but expand them: Get-Process | Select-Object -ExpandProperty Name
1, $null -ne $null # 1
1,2,3,4 -ge 3 #3, 4
'One', 'Two', 'Three' -like '*e*' # 'one', 'three'
$array = 1, 2, 3
if ($array -gt 3) {
Write-Host "Greater than 3"
}
# greater than 3

7. Redirection

In Bash, it has three major stream. stdout, stderr and stdin . It also has those concepts in the PowerShell as well.

function Test-Redirect {
'This is standard out'
Write-Error 'This is an error'
Write-Warning 'This is a warning'
}
$stdOut = Test-Redirect 3 > "warning.txt" 2> 'error.txt'
Get-Content 'Warning.txt'
# This is a warning
Get-Content 'error.txt'
# This is an error
Get-Process > $null
# or
Get-Process | out-null

8. Confirm/WhatIf/Force

Confirm , Force and Whatif are used to cooperate with other commands which are going to make change.

  • Confirm parameter will introduce a prompt before actually complete a job.
> cd $env:TEMP
> New-Item data.txt
> Remove-Item data.txt -Confirm
Confirm
Are you sure you want to perform this action?
Performing the operation "Remove File" on target "C:\\Users\\fenga\\AppData\\Local\\Temp\\data.txt".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
Clear-RecycleBin -Confirm:$false
> $ConfirmPreference
High
[CmdletBinding(ConfirmImpact='High')]
> $ConfirmPreference="Low"
> New-Item data.txt
Confirm
Are you sure you want to perform this action?
Performing the operation "Create File" on target "Destination: C:\\Users\\fenga\\AppData\\Local\\Temp\\data.txt".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
  • WhatIf presents with a simple statement that should state what would have been done.
> New-Item data.txt -Force
> Remove-Item data.txt -WhatIf
What if: Performing the operation "Remove File" on target "C:\\Users\\fenga\\AppData\\Local\\Temp\\data.txt".
$WhatIfPreference=$true
New-Item newfile.txt
What if: Performing the operation "Create File" on target "Destination: C:\\Users\\fenga\\AppData\\Local\\Temp\\newfile.txt".
function Test-ShouldProcess {
[CmdletBinding(SupportsShouldProcess)]
param()

if($pscmdlet.ShouldProcess("SomeObject", "delete")) {
Write-Host "Deleting SomeObject" -Foreground Cyan
}
}
Test-ShouldProcess
What if: Performing the operation "delete" on target "SomeObject".
  • Force parameter is widely used in command that avoid the prompt.
New-Item a.txt
Remove-Item a.txt -Force

9. Variable Scopes

PowerShell can limit the access to variables, aliases, functions by scope. It supports the following scopes:

  • Global: The scope takes effect when PowerShell starts or when you create a new session or runspace.
  • Local: The current scope. it can be the global scope or any other scope.
  • Script: It is created while a script file runs.
  • Scope may nest. An outer scope is referred to as the parents scope
  • A variable in the scope is visible in which it was created and in any child scopes. Unless you explicitly make it private.
  • A variable can be only changed in the scope in which it was created.
$local:thisValue = "Some Value"
"From Local: $local:thisValue" # From Local: Some Value
"From Global: $global:thisValue" # From Global: Some Value
function Test-ThisScope {
"From Local: $local:thisValue"
"From Global: $global:thisValue"
}
Test-ThisScope
# From Local:
# From Global: Some Value
$thisValue = "Some Value"function Test-ThisScope {
"From Local: $local:thisValue"
"From Global: $global:thisValue"
"Without scope: $thisValue"
}
Test-ThisScope
# From Local:
# From Global: Some Value
# Without scope: Some Value
$private:thisValue="Some Value"
"From global: $global:thisValue" #From global: Some Value
function Test-ThisScope {
"Without scope: $thisValue"
"From Private: $private:thisValue"
"From global: $global:thisValue"
}
Test-ThisScope
# Without scope:
# From Private:
# From global:

10. Dot Source

Script and functions follows all the rules of rule. But, you can add a script or function to the current scope by using dot source. By doing so, any function, variables that the script creates are available in the current scope.

# Sample.ps1
$ThisValue = "Hello World"
function Test-Hello {
Write-Host "This is from sample"
}
. ./Sample.ps1
$ThisValue # Hello World
Test-Hello # This is from sample

11. PSProvider / PSDriver

In PowerShell, the data has multiple source. They can be filesystem, registry, certificate, and so on.

PS> Get-PSProvider
Name Capabilities Drives
---- ------------ ------
Registry ShouldProcess {HKLM, HKCU}
Alias ShouldProcess {Alias}
Environment ShouldProcess {Env}
FileSystem Filter, ShouldProcess, Credentials {C,D, Temp}
Function ShouldProcess {Function}
Variable ShouldProcess {Variable}
> Get-PSDrive
Name Used (GB) Free (GB) Provider Root
---- --------- --------- -------- ----
Alias Alias
C 135.84 340.23 FileSystem C:\\
Cert Certificate \\
D 0.66 236.72 FileSystem D:\\
Env Environment
Function Function
HKCU Registry HKEY_CURRENT_USER
HKLM Registry HKEY_LOCAL_MACHINE
Temp 135.84 340.23 FileSystem C:\\Users\\gaufung\\AppData\\Local\\Tem…
Variable Variable
WSMan WSMan
Set-Location Cert:\\
Get-ChildItem
# Location : CurrentUser
# StoreNames : {AuthRoot, My, CA, Root…}
# Location : LocalMachine
# StoreNames : {Disallowed, SmartCardRoot, Trust, TestSignRoot…}

12. Comment-Based Help

As we all know, Get-Help command can help us display more details about a command. How can I leverage it when exporting our method to us? Yes, the answer is the MAML . It has several concepts:

  • .SYNOPSIS (Mandatory: Brief introduction for the function)
  • .DESCRPTION (Mandatory: Detailed introduction for the function)
  • .PARAMETER <Name>(Optional: parameter name explaination)
  • .EXAMPLE (Optional: example of this function usage)
  • .INPUTS (Optional)
  • .OUTPUTS (Optional)
  • .NOTES (Optional)
  • .LINK (Optional)
function Get-Something {
<#
.SYNOPSIS
Brifly describe the main action performed by Get-Something
.DESCRIPTION
A detailed descrption of the activities of Get-Something
#>
}
PS > get-help get-somethingNAME
Get-Something
SYNOPSIS
Brifly describe the main action performed by Get-Something
SYNTAX
Get-Something [<CommonParameters>]
DESCRIPTION
A detailed descrption of the activities of Get-Something
RELATED LINKSREMARKS
To see the examples, type: "Get-Help Get-Something -Examples"
For more information, type: "Get-Help Get-Something -Detailed"
For technical information, type: "Get-Help Get-Something -Full"

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store