PowerShell Tricks

Get-Process -Name <string[]>
Get-Process -Name @("powershell", "explorer")
Get-Process -Name powershell, explorer
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
function Get-Process {
param (
[string[]]Name,
[int[]]Id
)
}
$process = @{
Name = 'explorer'
}
Get-Process @process

@ instead of $

$object = [pscutomobject]@{"some name" = "value"}
$object."some name" # value
$object.{some name} # value
$prop = "some name"
$object.$prop # value
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
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
> 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"):
> 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".
New-Item a.txt
Remove-Item a.txt -Force
$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:
# 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
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…}
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"

--

--

--

A software developer in Microsoft at Suzhou. Most articles spoken language is Chinese. I will try with English when I’m ready

Love podcasts or audiobooks? Learn on the go with our new app.

The Evolution of Univers:

MongoDB Basic Syntax

how toReasons Why Images Not Loading On Your Website

Monthly Update — November 2020

Handy Python Snippets To Handle Random Things

A session with two best experts in the industry -> Mr. Sreejith Anujan & Mr. Arun Eapen.👨‍💻

Seeding My Database With A CSV File

What are you keeping from me?

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
Feng Gao

Feng Gao

A software developer in Microsoft at Suzhou. Most articles spoken language is Chinese. I will try with English when I’m ready

More from Medium

Windows Terminal — The end of multiple terminal windows

Forking a GitHub Repository using Git and VIM

Advance Concepts of Linux — PART-2