Tools
Package Repo and Management
PowerShell office (managed by MS) package repo: PowerShell Gallery.
Package manager: PowerShellGet
Install it with Install-Module -Name PowerShellGet -Force
.
Install package with Install-Module -Name ...
like the following ZLocation.
PowerShell directory auto jump
Install ZLocation with Install-Module -Name ZLocation
.
Like autojump
(j
) in shell, jump with z
command.
Development
Comment
#
for line;
<# ... #>
for block
Ref: https://stackoverflow.com/questions/7342597/how-do-you-comment-out-code-in-powershell
Test object type
write-host $res.GetType().fullname # write-host = println
if ($res -is [string]) {write-host "this is a string"}
if ($res -is [array]) {write-host "this is a array"}
Ref:
- http://thephuck.com/scripts/easy-way-to-check-if-your-powershell-variable-is-an-array-or-not/
List properties and methods of an object
Use Get-Member
:
$curTime = Get-Date # DateTime object
$timeStr = $curTime.ToString() # String
$dt = [datetime]::Parse($timeStr) # DateTime object
$dt | Get-Member # list props and methods
Convert result to array
Use @()
.
Ref: Powershell Test if array in one line
map
array
Or list comprehension. For example, map a string array to an object array:
$fileNameArr | % { Get-Item $_ }
.
Here %
means foreach
, $_
denote each element of the array.
Load variables from another powershell script
Use ./fileToLoad.ps1
.
Ref:
-
https://stackoverflow.com/questions/1864128/load-variables-from-another-powershell-script
-
https://stackoverflow.com/questions/27138483/how-can-i-re-use-import-script-code-in-powershell-scripts
File IO
Write to file
Write string $res to file path $lastResult: $res | out-file -encoding UTI8 $lastResult
Here -encoding
is mandatory, or the english characters will be written as full-width characters.
Write string $timeStr to file path $lastSync: [System.IO.File]::WriteAllLines($lastSync, $timeStr)
But why files created by these 2 methods are all have 'x' attributes?
Ref:
-
https://stackoverflow.com/questions/19122755/output-echo-a-variable-to-a-text-file
-
https://stackoverflow.com/questions/5596982/using-powershell-to-write-a-file-in-utf-8-without-the-bom
Head/tail of an array
Empty array: @()
All except first 3 items: 1..10 | Select-Object -skip 3
First 3 items: 1..10 | Select-Object -first 3
Last 3 items: 1..10 | Select-Object -last 3
Middle 4 items: 1..10 | Select-Object -skip 3 -first 4
Ref: https://stackoverflow.com/questions/10079572/powershell-equivalent-for-head-n-3
Get file attributes
$updated = Get-Date (Get-Item $fullname).LastWriteTime
Ref: https://stackoverflow.com/questions/19774097/finding-modified-date-of-a-file-folder
Print a certain line of a file with PowerShell
Or get a selected item from an index: (Get-Content file.txt)[4]
Ref: https://stackoverflow.com/questions/14759649/how-to-print-a-certain-line-of-a-file-with-powershell
Parse and format Time
default format
Get current time: $timeStr = Get-Date
Custom format
Get time from file and print as string:
$updated = Get-Date (Get-Item $fullname).LastWriteTime -format "yy.M.d H:m"
String -> DateTime -> String:
$y = [datetime]::ParseExact($createdStr, "yyyy-MM-dd HH:mm:ss", [Globalization.CultureInfo]::InvariantCulture)
$created = $y.ToString("yy.M.d H:m")
Ref:
-
https://technet.microsoft.com/en-us/library/ee692801.aspx
-
https://stackoverflow.com/questions/27741810/string-to-datetime-conversion-in-powershell
-
https://stackoverflow.com/questions/2249619/how-to-format-a-datetime-in-powershell
Run Powershell script in Winodws console
You have to add -file
flag for a non-command PS script:
powershell -file donshell.ps1 s reduce
Ref: https://stackoverflow.com/questions/13724940/how-to-run-a-powershell-script-from-the-command-line-and-pass-a-directory-as-a-p