Get-Help Get-Process | 명령어 도움말 |
Get-Help Get-Process -Examples | 예제 표시 |
Get-Command *process* | 명령어 찾기 |
Get-Alias | 모든 별칭 목록 |
Get-Member | 객체 속성 가져오기 |
$PSVersionTable | PowerShell 버전 |
Get-Location (pwd) | 현재 디렉토리 |
Set-Location path (cd) | 디렉토리 변경 |
Get-ChildItem (ls, dir) | 항목 나열 |
Get-ChildItem -Recurse | 재귀적 나열 |
Get-ChildItem -Filter *.txt | 확장자로 필터 |
Push-Location / Pop-Location | 스택 탐색 |
New-Item -ItemType File -Name "file.txt" | 파일 생성 |
New-Item -ItemType Directory -Name "folder" | 폴더 생성 |
Copy-Item source dest (cp) | 항목 복사 |
Move-Item source dest (mv) | 항목 이동 |
Remove-Item path (rm) | 항목 삭제 |
Remove-Item -Recurse -Force | 재귀적 폴더 삭제 |
Rename-Item old new | 항목 이름 변경 |
Test-Path path | 존재 여부 확인 |
Get-Content file.txt (cat) | 파일 읽기 |
Get-Content file.txt -Tail 10 | 마지막 10줄 |
Get-Content file.txt -Wait | 파일 추적 |
Set-Content file.txt "text" | 파일에 쓰기 |
Add-Content file.txt "text" | 파일에 추가 |
Clear-Content file.txt | 파일 내용 지우기 |
# Declare variable
$name = "John"
$age = 25
$pi = 3.14
$isValid = $true
# Type casting
[int]$number = "42"
[string]$text = 123
[datetime]$date = "2024-01-01"
# Get variable type
$name.GetType() # Create array
$arr = @(1, 2, 3, 4, 5)
$arr = 1..10 # Range
# Access elements
$arr[0] # First
$arr[-1] # Last
$arr[0..2] # First 3
# Operations
$arr += 6 # Add element
$arr.Count # Length
$arr | ForEach-Object { $_ * 2 } # Create hashtable
$hash = @{
Name = "John"
Age = 25
City = "NYC"
}
# Access values
$hash["Name"]
$hash.Name
# Add/Update
$hash["Country"] = "USA"
$hash.Remove("City")
# Iterate
$hash.GetEnumerator() | ForEach-Object {
"$($_.Key): $($_.Value)"
} # String interpolation
$name = "World"
"Hello, $name!" # Interpolated
'Hello, $name!' # Literal
# Here-strings
$text = @"
Multi-line
string here
"@
# String methods
$str.ToUpper()
$str.ToLower()
$str.Replace("old", "new")
$str.Split(",")
$str.Trim()
$str.Contains("sub") if ($x -gt 10) {
"Greater than 10"
} elseif ($x -eq 10) {
"Equals 10"
} else {
"Less than 10"
}
# Comparison operators
# -eq Equal
# -ne Not equal
# -gt Greater than
# -lt Less than
# -ge Greater or equal
# -le Less or equal
# -like Wildcard match
# -match Regex match switch ($value) {
1 { "One" }
2 { "Two" }
3 { "Three" }
default { "Unknown" }
}
# With wildcard
switch -Wildcard ($file) {
"*.txt" { "Text file" }
"*.ps1" { "Script file" }
}
# With regex
switch -Regex ($text) {
"^\d+$" { "Numbers only" }
"^\w+$" { "Word chars" }
} # For loop
for ($i = 0; $i -lt 10; $i++) {
$i
}
# Foreach loop
foreach ($item in $collection) {
$item
}
# ForEach-Object (pipeline)
1..10 | ForEach-Object {
$_ * 2
}
# Parallel (PowerShell 7+)
1..10 | ForEach-Object -Parallel {
$_ * 2
} -ThrottleLimit 5 # While loop
while ($x -lt 10) {
$x++
}
# Do-While
do {
$x++
} while ($x -lt 10)
# Do-Until
do {
$x++
} until ($x -ge 10)
# Break and Continue
foreach ($i in 1..10) {
if ($i -eq 5) { continue } # Skip
if ($i -eq 8) { break } # Exit loop
$i
} function Say-Hello {
param(
[string]$Name = "World"
)
"Hello, $Name!"
}
Say-Hello -Name "John" function Get-Square {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[ValidateRange(1, 100)]
[int]$Number
)
begin {
# Run once before pipeline
}
process {
# Run for each pipeline item
$Number * $Number
}
end {
# Run once after pipeline
}
}
1..5 | Get-Square function Get-Data {
$result = @()
$result += "Item 1"
$result += "Item 2"
return $result
}
# Multiple outputs
function Get-Info {
[PSCustomObject]@{
Name = "John"
Age = 25
}
} Get-Process | Where-Object { $_.CPU -gt 100 } | 객체 필터 |
Get-Process | Sort-Object CPU -Descending | 객체 정렬 |
Get-Process | Select-Object Name, CPU | 속성 선택 |
Get-Process | Select-Object -First 5 | 처음 5개 |
Get-Process | Group-Object Name | 객체 그룹화 |
Get-Process | Measure-Object -Property CPU -Sum | 통계 계산 |
Get-Process | Format-Table Name, CPU | 테이블로 포맷 |
Get-Process | Format-List * | 리스트로 포맷 |
Get-Process | Export-Csv process.csv | CSV로 내보내기 |
Get-Process | ConvertTo-Json | JSON으로 변환 |
Get-Process | 프로세스 목록 |
Get-Process -Name chrome | 특정 프로세스 |
Start-Process notepad | 프로세스 시작 |
Start-Process notepad -Wait | 시작 후 대기 |
Stop-Process -Name notepad | 프로세스 중지 |
Stop-Process -Id 1234 -Force | ID로 강제 중지 |
Get-Service | 서비스 목록 |
Get-Service -Name wuauserv | 특정 서비스 |
Start-Service -Name servicename | 서비스 시작 |
Stop-Service -Name servicename | 서비스 중지 |
Restart-Service -Name servicename | 서비스 재시작 |
Set-Service -Name service -StartupType Automatic | 시작 유형 설정 |
Test-Connection google.com | 호스트 핑 |
Test-NetConnection -Port 443 google.com | 포트 테스트 |
Invoke-WebRequest https://api.example.com | HTTP 요청 |
Invoke-RestMethod https://api.example.com/json | REST API 호출 |
Get-NetIPAddress | IP 주소 가져오기 |
Get-NetAdapter | 네트워크 어댑터 목록 |
# Enable remoting (admin)
Enable-PSRemoting -Force
# Connect to remote
Enter-PSSession -ComputerName Server01
# Run command remotely
Invoke-Command -ComputerName Server01 -ScriptBlock {
Get-Process
}
# Multiple computers
Invoke-Command -ComputerName Server01, Server02 -ScriptBlock {
$env:COMPUTERNAME
} try {
# Code that might fail
Get-Content "nonexistent.txt" -ErrorAction Stop
}
catch {
# Handle error
Write-Error "Error: $_"
$_.Exception.Message
}
finally {
# Always runs
"Cleanup"
}
# Error action preference
$ErrorActionPreference = "Stop" # Stop, Continue, SilentlyContinue, Inquire