PowerShell est un shell et langage de script orienté objets pour l’administration système et l’automatisation.
📁 Navigation et fichiers
| 📌 Action | 🧠 Commande |
|---|
| 📍 Répertoire courant | Get-Location ou pwd |
| 📂 Changer de répertoire | Set-Location C:\Path ou cd C:\Path |
| 📋 Lister les fichiers | Get-ChildItem ou ls |
| 🔍 Recherche récursive | Get-ChildItem -Recurse |
| 📄 Créer un fichier | New-Item -ItemType File fichier.txt |
| 📁 Créer un dossier | New-Item -ItemType Directory dossier |
| 📋 Copier un fichier | Copy-Item source.txt destination.txt |
| ➡️ Déplacer un fichier | Move-Item fichier.txt C:\Temp\ |
| 🗑️ Supprimer un fichier | Remove-Item fichier.txt |
🔧 Services et processus
| 📌 Action | 🧠 Commande |
|---|
| 📋 Lister les services | Get-Service |
| ▶️ Démarrer un service | Start-Service -Name "ServiceName" |
| ⏹️ Arrêter un service | Stop-Service -Name "ServiceName" |
| 🔄 Redémarrer un service | Restart-Service -Name "ServiceName" |
| 📊 Statut d’un service | Get-Service -Name "ServiceName" |
| 🔍 Lister les processus | Get-Process |
| ❌ Arrêter un processus | Stop-Process -Name "notepad" |
| 💻 Processus par CPU | Get-Process | Sort-Object CPU -Descending |
💾 Variables et types
| 📌 Action | 🧠 Syntaxe |
|---|
| 📝 Variable simple | $nom = "Jean" |
| 🔢 Variable typée | [int]$age = 30 |
| 📚 Tableau | $liste = @("item1", "item2", "item3") |
| 🗂️ Hash table | $hash = @{Nom="Jean"; Age=30} |
| 🔤 String | [string]$texte = "Hello" |
| ✅ Boolean | [bool]$vrai = $true |
| 📅 Date | $date = Get-Date |
| 🌐 Variable d’environnement | $env:COMPUTERNAME |
🔍 Filtrage et pipeline
| 📌 Action | 🧠 Commande |
|---|
| 🔎 Filtrer avec Where | Get-Service | Where-Object {$_.Status -eq "Running"} |
| 📊 Sélectionner colonnes | Get-Process | Select-Object Name, CPU |
| 📈 Trier les résultats | Get-Process | Sort-Object CPU -Descending |
| 📋 Premier élément | Get-Process | Select-Object -First 5 |
| 🎯 Chercher par nom | Get-Service | Where-Object {$_.Name -like "*win*"} |
| 📏 Mesurer les objets | Get-Process | Measure-Object |
| 🔄 ForEach dans pipeline | Get-Process | ForEach-Object {$_.Name} |
💬 Affichage et sortie
| 📌 Action | 🧠 Commande |
|---|
| 💬 Afficher du texte | Write-Host "Bonjour" |
| 📄 Sortie standard | Write-Output "Résultat" |
| ⚠️ Avertissement | Write-Warning "Attention" |
| ❌ Erreur | Write-Error "Erreur" |
| 📝 Vers un fichier | Get-Process | Out-File processus.txt |
| 📋 Vers le clipboard | Get-Process | Set-Clipboard |
| 🎨 Couleurs | Write-Host "Texte" -ForegroundColor Red |
🛠️ Gestion du système
| 📌 Action | 🧠 Commande |
|---|
| 💻 Infos système | Get-ComputerInfo |
| 🧠 Mémoire disponible | Get-CimInstance Win32_OperatingSystem |
| 💿 Espace disque | Get-CimInstance Win32_LogicalDisk |
| 👥 Utilisateurs locaux | Get-LocalUser |
| 🔐 Groupes locaux | Get-LocalGroup |
| 🌐 Configuration réseau | Get-NetAdapter |
| 📡 Adresses IP | Get-NetIPAddress |
| 🔄 Redémarrer l’ordinateur | Restart-Computer -Force |
🔄 Structures de contrôle
Conditions
# If/Else basique
if ($age -ge 18) {
Write-Host "Majeur"
} else {
Write-Host "Mineur"
}
# Switch simple
switch ($jour) {
"Lundi" { "Début de semaine" }
"Vendredi" { "Weekend proche" }
default { "Jour normal" }
}
Boucles
# ForEach simple
$services = Get-Service
foreach ($service in $services) {
Write-Host $service.Name
}
# For classique
for ($i = 1; $i -le 5; $i++) {
Write-Host "Compteur: $i"
}
📜 Fonctions et scripts
Fonction basique
function Get-SystemInfo {
param(
[string]$ComputerName = $env:COMPUTERNAME
)
Write-Host "Informations pour: $ComputerName"
Get-ComputerInfo | Select-Object TotalPhysicalMemory, CsProcessors
}
# Utilisation
Get-SystemInfo -ComputerName "MonPC"
Script avec paramètres
param(
[Parameter(Mandatory=$true)]
[string]$ServiceName,
[ValidateSet("Start", "Stop", "Restart")]
[string]$Action = "Start"
)
switch ($Action) {
"Start" { Start-Service -Name $ServiceName }
"Stop" { Stop-Service -Name $ServiceName }
"Restart" { Restart-Service -Name $ServiceName }
}
Write-Host "Action $Action effectuée sur $ServiceName"
🛠️ Dépannage et aide
| 🆘 Problème | 🧠 Solution |
|---|
| ❓ Aide sur une commande | Get-Help Get-Process -Examples |
| 🔍 Chercher une commande | Get-Command *service* |
| 📋 Voir les propriétés | Get-Service | Get-Member |
| 🚫 Erreur d’exécution | Set-ExecutionPolicy RemoteSigned |
| 📜 Historique commandes | Get-History |
| 🔧 Version PowerShell | $PSVersionTable |