Dasar
Perintah & Bantuan
Get-Help Get-Process # tampilkan bantuan cmdlet Get-Help Get-Process -Online # buka dokumen online Get-Command *service* # cari perintah berdasarkan nama Get-Alias ls # tampilkan target alias
Alias Umum
ls → Get-ChildItemDaftar file dan direktori
cd → Set-LocationGanti direktori
cp → Copy-ItemSalin file atau direktori
mv → Move-ItemPindahkan atau ganti nama
rm → Remove-ItemHapus file atau direktori
cat → Get-ContentBaca isi file
echo → Write-OutputCetak ke pipeline
cls → Clear-HostBersihkan konsol
Variabel
Dasar Variabel
$name = "Alice" # string $count = 42 # integer $pi = 3.14 # double $list = @(1, 2, 3) # array $hash = @{a=1; b=2} # hashtable
Variabel Otomatis
$_Objek pipeline saat ini
$PSVersionTableInformasi versi PowerShell
$HOMEDirektori home pengguna
$PWDDirektori saat ini
$nullNilai null
$true / $falseKonstanta boolean
$ErrorArray error terbaru
$LASTEXITCODEExit code perintah native terakhir
Variabel Environment
$env:PATH # baca env var $env:MY_VAR = "value" # set env var Get-ChildItem Env: # daftar semua env var
Operator
Operator Perbandingan
-eq -neSama / tidak sama
-gt -ltLebih besar / lebih kecil
-ge -leLebih besar/kecil atau sama
-like -notlikeCocok wildcard (*, ?)
-match -notmatchCocok regex
-containsKoleksi mengandung nilai
-in -notinNilai ada dalam koleksi
Operator Logika & Lainnya
-and -or -notOperator logika
!NOT logika (alias)
-replaceGanti regex: 'hi' -replace 'h','b'
-split -joinPecah / gabungkan string
..Range: 1..5 → 1,2,3,4,5
?:Ternary (v7+): $x ? 'yes' : 'no'
Alur Kontrol
if / elseif / else
if ($age -ge 18) { "Adult" } elseif ($age -ge 13) { "Teen" } else { "Child" }
switch
switch ($color) { "red" { "Stop" } "green" { "Go" } default { "Unknown" } }
Loop
foreach ($item in $list) { $item } for ($i=0; $i -lt 5; $i++) { $i } while ($x -lt 10) { $x++ } 1..5 | ForEach-Object { $_ * 2 }
Fungsi
Mendefinisikan Fungsi
function Get-Greeting { param([string]$Name = "World") "Hello, $Name!" } Get-Greeting -Name "Alice"
Parameter Lanjutan
function Copy-SafeFile { [CmdletBinding()] param( [Parameter(Mandatory)][string]$Path, [Parameter(Mandatory)][string]$Dest ) Copy-Item $Path $Dest -WhatIf:$WhatIfPreference }
Objek & Pipeline
Dasar Pipeline
Get-Process | Sort-Object CPU -Desc | Select-Object -First 5 Get-Service | Where-Object Status -eq "Running" Get-ChildItem | Measure-Object -Property Length -Sum
Cmdlet Pipeline Utama
Where-ObjectFilter objek: | Where {$_.CPU -gt 10}
Select-ObjectPilih properti: | Select Name, CPU
Sort-ObjectUrutkan: | Sort CPU -Desc
ForEach-ObjectTransformasi tiap objek: | ForEach {$_.Name}
Measure-ObjectHitung, jumlah, rata-rata, min, maks
Group-ObjectKelompokkan berdasarkan nilai properti
Format-TableTampilkan sebagai tabel: | ft -Auto
Export-CsvEkspor ke CSV: | Export-Csv out.csv
File
Operasi File
Get-Content log.txt # baca file Set-Content out.txt "hello" # tulis (timpa) Add-Content out.txt "more" # tambahkan Get-Content log.txt | Select-String "error" # grep
Cmdlet Path & File
Test-Path $pathPeriksa apakah file/direktori ada
New-Item -Type FileBuat file
New-Item -Type DirectoryBuat direktori
Resolve-PathDapatkan path absolut
Join-PathGabungkan segmen path
Split-PathDapatkan parent atau nama file
Get-ItemPropertyAtribut dan metadata file
Remove-Item -RecurseHapus direktori secara rekursif
String
Dasar String
"Hello, $name" # interpolasi (double quotes) 'Hello, $name' # literal (single quotes) "Length: $($list.Count)" # ekspresi dalam string @" Multi-line here-string with $name "@
Metode String
.ToUpper() / .ToLower()Ubah huruf besar/kecil
.Trim()Hapus whitespace awal/akhir
.Split(',')Pecah menjadi array
.Replace('a','b')Ganti substring
.StartsWith() / .EndsWith()Periksa prefix / suffix
.Substring(0,5)Ekstrak substring
.Contains('text')Periksa apakah mengandung
-f operator'{0} is {1}' -f 'sky','blue'
Modul
Manajemen Modul
Get-Module -ListAvailable # modul terinstal Find-Module -Name Az* # cari di gallery Install-Module -Name Pester # instal dari gallery Import-Module ActiveDirectory # muat modul
Perintah Modul
Get-ModuleDaftar modul yang dimuat
Import-ModuleMuat modul ke sesi
Remove-ModuleHapus modul dari sesi
Update-ModulePerbarui modul terinstal
Get-Command -Module XDaftar perintah dalam modul
$env:PSModulePathPath pencarian modul
Pola Umum
Penanganan Error
try { Get-Item "C:\missing" -ErrorAction Stop } catch { "Error: $_" } finally { "Cleanup here" }
Execution Policy & Remoting
Get-ExecutionPolicyTampilkan kebijakan script saat ini
Set-ExecutionPolicy RemoteSignedIzinkan script lokal
Enter-PSSession -Computer SRV1Sesi remote interaktif
Invoke-Command -Computer SRV1 -Script {}Jalankan script block dari jarak jauh
Start-Job { long-task }Jalankan background job
Receive-Job -Id 1Dapatkan output background job