Create Object


# example 1
$var = New-Object System.DateTime

# example 2
$newObject = [PSCustomObject]@{
	ComputerName	= 'Server1'
	Role		= 'Interface'
	Environment	= 'Production'
}


Split variable

$intput = "foo.baz,bar"
$part = $input.Split(".")
$fooVar = $part[0]
$bazVar = $part[1]
$barVar = $part[2]

# simply do this
$foo, $baz, $bar = $input.Split(".")

# whatif number of variables is less than splits.
$foo, $sec = $input.Split(".")
# $foo == foo, $sec = ["baz", "bar"]

Scope(global and local)

$foo = "global"
function myFunc {
	$foo = "local"
	Write-Host $global:foo 	# global
	Write-Host $local:foo 	# local
	Write-Host $foo		# local
}
# call function
myFunc
Write-Host $local:foo 	# global
Write-Host $foo 	# global

Remove Variable


$var = "something in here"

# remove it
Remove-Variable -Name var
# or
rv var

Operators

Comparision Operators

-eq ==
-ne !=
-gt >
-ge >=
-lt <
-le <=

# example 
# 5 -eq 5

#-----------------------------

# String comparison operators
"myString" -like "*String"
"myString" -nolike "Other*"
"myString" -match '^String$'
"myString" -nomatch '^Other$'


#-----------------------------

# Collection comparison operators
"abc", "def" -contains "def" 		# return true
"abc", "def" -notcontains "123"		# return true
"def" -in "abc", "def"			# return true
"123" -notin "abc", "def"		# return true

Arithmetic Operators

+
-
-1 # set negative value
*
/
%

# Bitwise
100 -shl 2 # Bitwise Shift-left
100 -shr 1 # Bitwise Shift-right

#------------------------------
# Assignment Operators
$var = 1
$var += 1

...

$var++
$var--


Mixing operand types


# For Addition
"4" + 2 	# 42
4 + "2" 	# 6
1,2,3 + "Hello"	# 1,2,3,"Hello"
"Hello" + 1,2,3 # "Hello1 2 3"

# For Multiplication
"3" * 2 	# "33"
2 * "3"		# 6
1,2,3 * 2	# 1,2,3,1,2,3
2 * 1,2,3	# error

Read Input


$a = Read-Host "Enter a number:"
# assigned value into $a


Logical Operator


-and 
-or 
-xor 	# exclusive or
-not 
! 	# not

Replace Operator


"The rain in Seattle." -replace 'rain' 'hail' # return "The hail in Seattle."


split and join


"A B C" -split " "  		# Return an array string

"E", "F", "G" -join ":" 	# Return a single string, E,F,G

Where

Where-Object
where
?


Select


$dir = dir "C:\myDir"

$dir | select Name, FullName, Attributes

Foreach

shorthand syntax == %
$Numbers = ForEach ($Number in 1..20) {
	$Number
}
(1..10).ForEach{$_ * $_}
# output
1
4
9
16
25

...

Break Labels

$i = 0
:mainLoop While($i -lt 15) {
	$j = 0
	While($j -lt 15) {
		if($i -gt 10) {
			break mainLoop
		}
		$j++
	}
	$i++
}

while application

Start-Process notepad.exe
while(Get-Process notepad -ErrorAction SilentlyContinue) {
	Start-Sleep -Milliseconds 500
}

switch

switch($val){
	'First condition' 	{First action}
	'Second condition'	{Second action}
	Default			{}
}
Case Sensitive
switch -CaseSensitive ('Condition'){
	'condition' 	{First action}
	'Condition'	{Second action}
	Default		{}
}
# output: Second action
Wildcard
switch -Wildcard ('Condition') {
	'Condition'		{first}
	'Condit*'		{second}
	'C[aeo]ndit[f-l]on'	{third}
	'C?ndition'		{fourth}
	'Test*'			{fifth}
	Default			{}
}
File
# input.txt
# condition
# test

switch -file input.txt
{
	'condition'	{First}
	'test'		{Second}
	'fail'		{Third}
	Default		{}
}

# output
# First
# Second

Special characters


`0	# NUll
`a 	# Alert/Beep
`b 	# Backspace
`f 	# Form feed (used for printer output)
`n 	# New line
`r	# Carriage return
`t 	# Horizontal tab
`v 	# Vertical tab (used for printer output)


`#	# Comment-operator
`$	# Variable operator
``	# Escape character
`'	# Single quote
`"	# Double quote


Create new object


$newArr = @()
$newArr += New-Object -TypeName PSObject -Property @{
	Name 	= $env:username
	ID	= 12
	Address	= $null
}


Function with Parameters


function write-greeting {
	param(
		[Parameter(Mandatory, Position=0)]
		[String]$name
		[Parameter(Mandatory, Position=1)]
		[Int]$age
	)
	"Hello $name, you are $age years old"
}



or simple function



function write-greeting($name, $age) {
	"Hello $name, you are $age years old"
}




# The order of the parameters on the invocation 
# is matched to the order of the declaration in the function header(by default),
# or can be specified using the Position Parameter Attribute.
$greeting = Write-Greeting "Jim" 82

# Alternatively
$greeting = Write-Greeting -name "Bob" -age 82

cmdletbinding - function will behave like a cmdlet

function Verb-Noun
{
	[Cmdletbinding(DefaultParameterSetName='Parameter Set 1', 
		SupportsShouldProcess=$true,
		PositionalBinding=$false,
		HelpUri = 'http://www.microsoft.com/',
		ConfirmImpact='Medium'
	)]

	...

}

Mandatory Parameters

function Get-Greeting {
	param
	(
		[Parameter(Mandatory=$true)]$name
	)
	"Hello World $name"
}
# the command line will prompt for the value
$greeting = Get-Greeting 

ValidateSet

Need to restrict the possible values that a parameter can accept.
param(
	[ValidateSet('red', 'green', 'blue', IgnoreCase)]
	[string]$Color
) 

ValidateRange

param(
	[ValidateRange(0,120)]
	[Int]$Age
) 

ValidatePattern

param(
	[ValidatePattern("\w{4-6}\d{2}")]
	[string]$UserName
) 

Validatelength

param(
	[ValidateLength(0,15)]
	[String]$PhoneNumber
) 

ValidateCount

This method of parameter validation tests the amount of arguments passed in, for example, an array of strings.
param(
	[ValidateCount(1,5)]
	[String[]]$ComputerName
) 

ValidateScript

Finally, the ValidateScript method is extraordinarily flexible, taking a scriptblock and evaluating it using $_ to represent the passed argument. It then passes the argument if the result is $true (including any output as valid).

This can be used to test that a file exists:
param(
	[ValidateScript({Test-Path $_})]
	[IO.FileInfo]$Path
) 
param(
	[ValidateScript({Get-ADUser $_})]
	[String]$UserName
) 

listing available constructors for a class

You can list available constructors by calling the static new-method without parethese.
[DateTime]::new
OverloadDefinitions                                                            
-------------------                                                            
datetime new(long ticks)                                                       
datetime new(long ticks, System.DateTimeKind kind)                             
datetime new(int year, int month, int day)                                     
datetime new(int year, int month, int day, System.Globalization.Calendar       
calendar)                                                                      
datetime new(int year, int month, int day, int hour, int minute, int second)   
datetime new(int year, int month, int day, int hour, int minute, int second,   
System.DateTimeKind kind)                                                      
datetime new(int year, int month, int day, int hour, int minute, int second,   
System.Globalization.Calendar calendar)                                        
datetime new(int year, int month, int day, int hour, int minute, int second,   
int millisecond)                                                               
datetime new(int year, int month, int day, int hour, int minute, int second,   
int millisecond, System.DateTimeKind kind)                                     
datetime new(int year, int month, int day, int hour, int minute, int second,   
int millisecond, System.Globalization.Calendar calendar)                       
datetime new(int year, int month, int day, int hour, int minute, int second,   
int millisecond, System.Globalization.Calendar calendar, System.DateTimeKind   
kind)                 

Methods and properties

class Person {
	[string] $FirstName
	[string] $LastName
	[string] Greeting() {
		return "Greetings, {0} {1}" -f $this.FirstName, $this.LastName
	}
}

$x = [Person]::new()
$x.FirstName = "Jane"
$x.LastName = "Doe"
$greeting = $x.Greeting() # output: "Greetings, Jane Doe!"

Overloading

class Person {
	[string] $name
	[int] $age

	Person([string] $name) {
		$this.name = $name
	}

	Person([string]$name, [int]$age) {
		$this.name = $name
		$this.age n $age
	}
}

Inheritance from Parent class to Child class

class ParentClass 
{
	[string] $meg = "It's under the parent class"
	[string] getMessage()
	{
		return ("Message: {0}" -f $this.meg)
	}
}

# bar extends Foo and inherits its members
class ChildClass : ParentClass
{
	
}


$Inherit = [ChildClass]::new()
so, $Inherit.meg will give you the "It's under the Parent Class"

Create a Module Manifest

@{
	RootModule = 'myModule.psm1'
	ModuleVersion = '1.0'
	CompatiblePSEditions = @('Core')
	GUID = '6382 ... 32x'
	Author = 'Bob'
	CompanyName = 'myCompany'
	Copyright = '(c) 2018 all rights'
	Description = 'It's description.'
	FunctionsToExport = @()
	CmdletsToExport = @()
	VariablesToExport = @()
	AliasesToExport = @()
	DscResourcesToExport = @()
} 

Simple Module Example

function Add {
	[CmdletBind()]
	param (
		[int] $x
		[int] $y
	)

	return $x + $y
}

Export-ModuleMember -Function Add

PowerShell profiles

A PowerShell profile is used to load user defined variables and functions automatically.
PowerShell profiles are not automatically created for users.
$PROFILE | Format-List -Force
$PROFILE.AllUsersAllHosts

Calculated Properties

Display file properties.
Get-ChildItem -Path C:\myDir | Select-Object Name, CreateTim, Length 

# calcualted properties
Get-ChildItem -Path C:\myDir | Select-Object Name, @{name="Size_In_KB"; Expression={$_.Length / 1Kb}}



Using the .Net Math Class

Knowing which methods are available you can use:
[System.Math] | Get-Member -Static -MemberType Methods
Examples
[System.Math]::Floor(9.33) # 9
[System.Math]::Ceiling(9.333) # 10
[System.Math]::Pow(4,3) # 64
[System.Math]::Sqrt(49) # 7

Variables

$PSItem

Same as $_.
Get-Process | ForEach-Object -Process {
	$PSItem.Name
}

$?

Contains the execution status of the last operation.
It contains TRUE if the last operation succeeded and FALSE if it failed.
Contains status of the last operation. When there is no error, it is set to Treu.
Write-Host "yes yes"
$? # True
Get-Process -Name nooo
Write-Host -Object "Was the last operation successful? $?"

$error

Contains an array of error objects that represent the most recent errors.
Get-Process -Name noooo
Write-Host -Object ('The last error that occurred was: {0}', -f $error[0].Exception.Message)
Remove error
$error.remove($error[0]) 

$OFS

Converting an array to a string. By default $OFS = " "
$arr = 1, 2, 3
$OFS = ",."

$arr # 1,.2,.3

Contains process ID of the current hosting process.
$pid 

Splatting parameters

$MyParameters = @{
	Name = "iexplore"
	FileVersionInfo = $true
}
# splatting Get-Process @MyParameters # without splatting Get-Process -Name "iexplore" -FileVersionInfo

Write Preferences

Write-Verbose "Detailed message"
Write-Information "Information Message"
Write-Debug "Debug Message"
Write-Progress "Progress Message"
Write-Warning "Warning Message"

Basic job creation

# Start a Script Block as background job: 
$job = Start-Job -ScriptBlock {Get-Process}

# Start a script as background job
$job = Start-Job -FilePath "C:\yourScript.ps1"

Error Handle

Try
{
	... Something Error ...
	Stop-Process -Id 123456 -ErrorAction Stop
}
Catch
{
	... Do Something ...
	"Non-Terminating Error: Invalid Process ID"
}

Credential

輸出加密檔案xml
$credential = get-credential
$credential | Export-CliXml -Path 'C:\yourPath\cred.xml'
import加密檔案
$credential = Import-CliXml -Path 'C:\yourPath\cred.xml' 

get-command


# It will display all s*.
get-command -Noun s*

Write-Host 和 Write-Output 差異

Write-Host # 只提供stdout功能(只有輸出) 
Write-Output # 提供stdout和redirection

# example
$msg = Write-Output "Hello World!"
$msg
# output
# "Hello World!"

# alias "Echo" and "Write"

Set - Alias


Create a directory

Setting type with "-itemtype" and pass the parameter "directory".
New-Item -itemtype directory ExampleFolderName

sort

Example:
Get-ChildItem ./ | Sort-Object
# or
Get-ChildItem ./ | Sort

Tee

This command can save results in file or variable by pipelining.
Get-Process | Tee-Object -FilePath "C:\result.txt"
Get-Process notepad | Tee-Object -Variable prog