Skip to content
> 💻 🧠 קוד 1001 > 📑 דפי עזר > פקודות PowerShell חיוניות

פקודות PowerShell חיוניות

1. ניהול קבצים ותיקיות

Get-ChildItem (gci / ls / dir) – מציג רשימת קבצים ותיקיות.
תחביר:

Get-ChildItem [-Path] <string> [-Recurse] [-Force]

דוגמאות:

Get-ChildItem                     # הצגת פריטים בתיקייה הנוכחית
Get-ChildItem -Path C:\Users -Recurse  # הצגת כל הפריטים באופן רקורסיבי
Get-ChildItem -Force              # לכלול קבצים מוסתרים

Set-Location (cd / sl) – שינוי תיקייה נוכחית.
תחביר:

Set-Location [-Path] <string>

דוגמאות:

Set-Location C:\Users   # מעבר לתיקיית Users
Set-Location ..         # מעבר לתיקיית האב

Copy-Item (cp / copy) – מעתיק קבצים או תיקיות.
תחביר:

Copy-Item [-Path] <string> [-Destination] <string> [-Recurse] [-Force]

דוגמאות:

Copy-Item .\file.txt C:\Backup\file.txt
Copy-Item .\* C:\Backup -Recurse  # העתקת כל הקבצים באופן רקורסיבי

Move-Item (mv / move) – מעביר קבצים או תיקיות.
תחביר:

Move-Item [-Path] <string> [-Destination] <string> [-Force]

דוגמאות:

Move-Item .\file.txt C:\Archive\file.txt

Remove-Item (rm / del / rmdir) – מוחק קבצים או תיקיות.
תחביר:

Remove-Item [-Path] <string> [-Recurse] [-Force]

דוגמאות:

Remove-Item .\temp.txt                        # מחיקת קובץ
Remove-Item C:\Temp -Recurse -Force          # מחיקת תיקייה עם כל התוכן

New-Item – יוצר קובץ או תיקייה חדשה.
תחביר:

New-Item [-Path] <string> [-ItemType <string>]

דוגמאות:

New-Item -Path . -Name "NewFolder" -ItemType Directory
New-Item -Path . -Name "file.txt" -ItemType File

2. ניהול מערכת ותהליכים

Get-Process (ps / gps) – מציג רשימת תהליכים פעילים.
תחביר:

Get-Process [-Name] <string>

דוגמאות:

Get-Process                     # הצגת כל התהליכים
Get-Process -Name notepad        # הצגת תהליך notepad בלבד

Stop-Process (kill) – עוצר תהליך.
תחביר:

Stop-Process [-Name] <string> [-Id] <int> [-Force]

דוגמאות:

Stop-Process -Name notepad
Stop-Process -Id 1234 -Force

Get-Service – מציג רשימת שירותי Windows.
תחביר:

Get-Service [-Name] <string>

דוגמאות:

Get-Service                     # רשימת כל השירותים
Get-Service -Name wuauserv       # הצגת שירות ספציפי

Start-Service / Stop-Service – הפעלה או עצירה של שירות.
תחביר:

Start-Service [-Name] <string>
Stop-Service [-Name] <string>

דוגמאות:

Start-Service wuauserv
Stop-Service wuauserv

Restart-Computer – הפעלה מחדש של המערכת.
תחביר:

Restart-Computer [-Force] [-Wait] [-Delay <int>]

דוגמא:

Restart-Computer -Force

Get-EventLog – אחזור יומני אירועים.
תחביר:

Get-EventLog -LogName <string> [-Newest <int>]

דוגמא:

Get-EventLog -LogName System -Newest 10   # 10 האירועים האחרונים במערכת

3. ניהול רשת

Test-Connection (ping) – בדיקת חיבור למחשב אחר.
תחביר:

Test-Connection [-ComputerName] <string> [-Count <int>]

דוגמא:

Test-Connection google.com -Count 4

Get-NetIPAddress – הצגת תצורת IP.
תחביר:

Get-NetIPAddress

דוגמא:

Get-NetIPAddress

Resolve-DnsName – שאילתה ל-DNS.
תחביר:

Resolve-DnsName [-Name] <string>

דוגמא:

Resolve-DnsName google.com

Get-NetTCPConnection – הצגת חיבורי TCP (כמו netstat).
תחביר:

Get-NetTCPConnection

דוגמא:

Get-NetTCPConnection

4. משתנים וסביבה

Get-Variable / Set-Variable / Remove-Variable – ניהול משתני PowerShell.
תחביר:

Get-Variable -Name <string>
Set-Variable -Name <string> -Value <object>
Remove-Variable -Name <string>

דוגמא:

Set-Variable -Name myVar -Value 42
Get-Variable -Name myVar
Remove-Variable -Name myVar

Get-ChildItem Env: – הצגת משתני סביבה.
תחביר:

Get-ChildItem Env:

דוגמא:

Get-ChildItem Env:PATH

5. פלט ועיצוב

Write-Output / Write-Host – הדפסת טקסט במסוף.
תחביר:

Write-Output "text"
Write-Host "text"

דוגמא:

Write-Output "שלום עולם"
Write-Host "אזהרה!" -ForegroundColor Red

Format-Table / Format-List – עיצוב הפלט.
דוגמא:

Get-Process | Format-Table -AutoSize
Get-Service | Format-List Name, Status

Out-File / Tee-Object – הפנייה לקובץ.
דוגמא:

Get-Process | Out-File C:\temp\processes.txt
Get-Process | Tee-Object -FilePath C:\temp\processes.txt

6. סקריפטינג וזרימת בקרה

If / Else – תנאי.
דוגמא:

if (Test-Path "C:\temp\file.txt") {
    Write-Host "הקובץ קיים"
} else {
    Write-Host "הקובץ לא נמצא"
}

ForEach-Object / ForEach – חזרה על אוסף.
דוגמא:

Get-ChildItem | ForEach-Object { Write-Host $_.Name }

Switch – תנאי עם מספר אפשרויות.
דוגמא:

switch ($day) {
    "Mon" { Write-Host "שני" }
    "Tue" { Write-Host "שלישי" }
}

Try / Catch / Finally – טיפול בשגיאות.
דוגמא:

try {
    Remove-Item "C:\temp\nofile.txt"
} catch {
    Write-Host "הקובץ לא נמצא"
} finally {
    Write-Host "הפעולה הסתיימה"
}

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *