Skip to content
> 💻 🧠 Codice 1001 > 📑 Schede Riassuntive > Comandi essenziali di PowerShell

Comandi essenziali di PowerShell

Отлично! Вот полный текст статьи Essential PowerShell Commands на итальянском, с сохранением структуры, синтаксиса и примеров:


Comandi Essenziali di PowerShell

1. Gestione di File e Directory

Get-ChildItem (gci / ls / dir) – Elenca file e directory.
Sintassi:

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

Esempi:

Get-ChildItem               # Elenca elementi nella directory corrente
Get-ChildItem -Path C:\Users -Recurse  # Elenca ricorsivamente gli elementi
Get-ChildItem -Force        # Include i file nascosti

Set-Location (cd / sl) – Cambia la directory corrente.
Sintassi:

Set-Location [-Path] <string>

Esempi:

Set-Location C:\Users   # Vai nella cartella Users
Set-Location ..         # Vai alla directory superiore

Copy-Item (cp / copy) – Copia file o cartelle.
Sintassi:

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

Esempi:

Copy-Item .\file.txt C:\Backup\file.txt
Copy-Item .\* C:\Backup -Recurse  # Copia tutti i file ricorsivamente

Move-Item (mv / move) – Sposta file o directory.
Sintassi:

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

Esempi:

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

Remove-Item (rm / del / rmdir) – Elimina file o directory.
Sintassi:

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

Esempi:

Remove-Item .\temp.txt                        # Elimina un file
Remove-Item C:\Temp -Recurse -Force          # Elimina la cartella con tutto il contenuto

New-Item – Crea un nuovo file o directory.
Sintassi:

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

Esempi:

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

2. Gestione di Sistema e Processi

Get-Process (ps / gps) – Elenca i processi in esecuzione.
Sintassi:

Get-Process [-Name] <string>

Esempi:

Get-Process                     # Mostra tutti i processi
Get-Process -Name notepad        # Mostra solo notepad

Stop-Process (kill) – Termina un processo.
Sintassi:

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

Esempi:

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

Get-Service – Elenca i servizi di Windows.
Sintassi:

Get-Service [-Name] <string>

Esempi:

Get-Service                     # Elenca tutti i servizi
Get-Service -Name wuauserv       # Mostra un servizio specifico

Start-Service / Stop-Service – Avvia o ferma un servizio.
Sintassi:

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

Esempi:

Start-Service wuauserv
Stop-Service wuauserv

Restart-Computer – Riavvia il sistema.
Sintassi:

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

Esempio:

Restart-Computer -Force

Get-EventLog – Recupera i log eventi.
Sintassi:

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

Esempio:

Get-EventLog -LogName System -Newest 10   # Ultimi 10 eventi di sistema

3. Gestione della Rete

Test-Connection (ping) – Verifica la connettività.
Sintassi:

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

Esempio:

Test-Connection google.com -Count 4

Get-NetIPAddress – Mostra configurazione IP.
Sintassi:

Get-NetIPAddress

Esempio:

Get-NetIPAddress

Resolve-DnsName – Query DNS.
Sintassi:

Resolve-DnsName [-Name] <string>

Esempio:

Resolve-DnsName google.com

Get-NetTCPConnection – Mostra connessioni TCP (simile a netstat).
Sintassi:

Get-NetTCPConnection

Esempio:

Get-NetTCPConnection

4. Ambiente e Variabili

Get-Variable / Set-Variable / Remove-Variable – Gestione variabili PowerShell.
Sintassi:

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

Esempio:

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

Get-ChildItem Env: – Elenca variabili di ambiente.
Sintassi:

Get-ChildItem Env:

Esempio:

Get-ChildItem Env:PATH

5. Output e Formattazione

Write-Output / Write-Host – Stampa testo su console.
Sintassi:

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

Esempio:

Write-Output "Hello World"
Write-Host "Attenzione!" -ForegroundColor Red

Format-Table / Format-List – Formatta output.
Esempio:

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

Out-File / Tee-Object – Reindirizza output su file.
Esempio:

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

6. Scripting e Controllo del Flusso

If / Else – Esecuzione condizionale.
Esempio:

if (Test-Path "C:\temp\file.txt") {
    Write-Host "File esiste"
} else {
    Write-Host "File non trovato"
}

ForEach-Object / ForEach – Loop su collezione.
Esempio:

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

Switch – Multi-condizione.
Esempio:

switch ($day) {
    "Mon" { Write-Host "Lunedì" }
    "Tue" { Write-Host "Martedì" }
}

Try / Catch / Finally – Gestione errori.
Esempio:

try {
    Remove-Item "C:\temp\nofile.txt"
} catch {
    Write-Host "File non trovato"
} finally {
    Write-Host "Operazione terminata"
}

Leave a Reply

Your email address will not be published. Required fields are marked *