.NET은 마이크로소프트(Microsoft)에서 만든 범용 소프트웨어 개발 프레임워크로, 여러 언어(C#, VB.NET, F# 등)와 다양한 플랫폼(Windows, Linux, macOS 등)을 지원합니다.
PowerShell은 바로 이 .NET 위에서 동작하는 자동화 및 스크립트 환경입니다.
Add-Type)PowerShell은 .NET 클래스, 메서드, 속성을 직접 호출할 수 있는 고유한 스크립트 언어입니다.
[네임스페이스.클래스이름]::정적메서드(인자)
(인스턴스).메서드()
예:
[System.Math]::Sqrt(16) # 정적 메서드
(Get-Date).AddDays(7) # 인스턴스 메서드
| 기능 | .NET 클래스 | PowerShell 예제 |
|---|---|---|
| 파일 읽기 | System.IO.File | [IO.File]::ReadAllText("C:\file.txt") |
| 디렉터리 정보 | System.IO.Directory | [IO.Directory]::GetFiles("C:\Logs") |
| 문자열 처리 | System.Text.StringBuilder | $sb = New-Object Text.StringBuilder |
| 날짜 계산 | System.DateTime | (Get-Date).AddDays(5) |
| 웹 요청 | System.Net.WebClient, System.Net.Http.HttpClient | $wc = New-Object Net.WebClient; $wc.DownloadString("http://example.com") |
| 암호화 | System.Security.Cryptography | 다양한 해시 생성 등 가능 |
Add-Type -TypeDefinition @"
public class Calculator {
public static int Add(int a, int b) => a + b;
}
"@
[Calculator]::Add(3, 5)
PowerShell은
.NET 컴파일 기능을 내장하여 스크립트 내에서 C# 코드를 정의하고 사용할 수 있음
Get-Process | Where-Object { $_.CPU -gt 50 }
→ .NET의 System.Diagnostics.Process 클래스로부터 파생된 객체를 다룸
$client = [System.Net.Http.HttpClient]::new()
$response = $client.GetStringAsync("https://api.github.com").Result
$md5 = [System.Security.Cryptography.MD5]::Create()
$bytes = [System.Text.Encoding]::UTF8.GetBytes("Hello")
$hash = $md5.ComputeHash($bytes)
[BitConverter]::ToString($hash)
| 항목 | .NET Framework | .NET Core | .NET 5~8 |
|---|---|---|---|
| 플랫폼 | Windows 전용 | 크로스 플랫폼 | 크로스 플랫폼 (통합 버전) |
| PowerShell 버전 | Windows PowerShell 5.1 | PowerShell Core 6/7 | PowerShell 7+ |
| 지원 상태 | 유지보수 중단 예정 | 유지보수 | 적극 개발 중 |
System.IO.File 같은 도구로 파일도 읽고, System.Net.WebClient로 인터넷도 하고, Math로 계산도 하죠!.NET 클래스와 구조를 반드시 이해해야 함.NET 기반 객체 조작으로 귀결됨