-
Start
-
Enable Unsigned scripts execution
- set-executionpolicy Unrestricted
-
Output
-
Console
- Write-Host "Hello Line!"
- "Hello Line!"
- Write-Output "Hello Line!"
- Outputs Hello Line!
-
Strings
-
Interpolation
- $x = 10
"Hello $x!"
====== Out:
Hello 10!
- @" This is
Multiline
string"@
-
GUI
- BEST! - PowerGUI
-
Date And Time
-
(Get-Date).ToString()
=========
3/3/2014 21:41:38
- Get-Date
-
Files
-
Append file
- Out-File "C:\logfile.txt" -Append
-
Path
- if (!(test-path $targetdir)) {
md $targetdir
}
- join-path System *ControlSet* -resolve
- Resolve-Path .
-
Read
- $templateContent = Get-Content $viewFileName | Out-String
-
Statements
-
Loops
- for ($i = 3; $i -lt 100; $i++) { ... }
- Get-Service | ForEach {$_.Name}
-
Functions
-
function Hello ($a, $b, $c)
{
"Hello $a, $b, $c"
}
Hello "111" "10" "20"
-
Parameters
- function Hello
{
($a, $b, $c) = $args
"Hello $a, $b, $c"
}
Hello "111" "10" "20"
- function Hello
{
param($a, $b, $c)
"Hello $a, $b, $c"
}
- Param ([int]$x = 0, [int]$y = 0)
- Call: Hello -b "B" -a "A" -c "CCCC"
- Return Value is
Hello 111, 10, 20
-
Filters
- Get-Service | ForEach {$_.Name}
- Get-Service | Where {$_.Status -eq "Running"}
-
Services
- get-service lanmanserver | Foreach { start-service $_.name -passthru; start-service $_.DependentServices -passthru}
- restart-service spooler -PassThru
- Stop-Service -Name Spooler
- Start-Service -Name Spooler
-
Process
- Get-Process
-
Run External Programm
-
How do I capture the output into a
variable from an external process in Powershell?
- $OutputVariable = (Shell command) | Out-String
-
With spaces in path
- Call operator: & ' comm and'
-
Process - Output
-
Out-String
- get-alias | out-string -stream | select-string "Get-"
- As Array
-
Wait Process finished
- $proc = Start-Process (MsiExec.exe /qn /x$guild /log "$logname") -PassThru;
$proc | Wait-Process
- (Start-Process -FilePath msiexec.exe -ArgumentList "/i no.msi /quiet /qb!" -Wait -Passthru).ExitCode
-
Environment
- $($env:COMPUTERNAME)
-
Regular Expressions
-
Regular Expressions with Windows PowerShell
- 'test' -match '\w'
-
Appendix B. Regular Expression Reference
- PS > "Test" -match '[Tes]'
True
- PS > 'ATTT' -match 'AT+'; $Matches[0]
True
ATTT
- $_ -replace "\.crl$", "+.crl"
-
Resources
-
PowerShell Pro!
-
Variables, Arrays, and Hash Tables
-
Arrays
- $x = @(1, 2, 3, 4, 5)
$y = @(6, 7, 8, 9, 10)
$z = $x + $y
$z<enter>
-
Hash Tables
- $EmpNumbers = @{“John Doe” = 112233;
“Dave Davis” = 223344;
“Justine Smith” = 334455}
- PowerShell Functions and Filters – The basics
- Функции и фильтры в PowerShell
- Commands Reference
-
Learn Powershell
- Learn PowerShell – Piping