windowspowershelladministration
MDstable
NoteSnippetChecklistPlaybook
Administration PowerShell
Commandes PowerShell essentielles pour l'administration Windows Server
snippetbeginner 2025-05-10 3 min read
windowspowershelladministrationservercmdlet
Navigation et fichiers
powershell
# NavigationSet-Location C:\WindowsGet-Location # pwdGet-ChildItem # ls / dirGet-ChildItem -Recurse -Filter "*.log"# FichiersNew-Item -ItemType File fichier.txtNew-Item -ItemType Directory dossierCopy-Item source.txt dest.txtMove-Item ancien.txt nouveau.txtRemove-Item fichier.txtRemove-Item dossier -Recurse -Force# ContenuGet-Content fichier.txtGet-Content fichier.txt -Tail 50 # dernières 50 lignesSet-Content fichier.txt "contenu"Add-Content fichier.txt "nouvelle ligne"
Processus et services
powershell
Variables
{{SERVICE}}
{{PID}}
# ProcessusGet-ProcessGet-Process -Name {{SERVICE}}Stop-Process -Name {{SERVICE}} -ForceStop-Process -Id {{PID}} -ForceStart-Process notepad.exe# ServicesGet-ServiceGet-Service -Name {{SERVICE}}Start-Service {{SERVICE}}Stop-Service {{SERVICE}}Restart-Service {{SERVICE}}Set-Service {{SERVICE}} -StartupType AutomaticSet-Service {{SERVICE}} -StartupType Disabled# Services en coursGet-Service | Where-Object {$_.Status -eq "Running"}Get-Service | Where-Object {$_.StartType -eq "Automatic" -and $_.Status -eq "Stopped"}
Utilisateurs et groupes locaux
powershell
Variables
{{USER}}
{{PASSWORD}}
# Utilisateurs locauxGet-LocalUserNew-LocalUser -Name "{{USER}}" -Password (ConvertTo-SecureString "{{PASSWORD}}" -AsPlainText -Force)Remove-LocalUser -Name "{{USER}}"Enable-LocalUser -Name "{{USER}}"Disable-LocalUser -Name "{{USER}}"# Groupes locauxGet-LocalGroupGet-LocalGroupMember -Group "Administrators"Add-LocalGroupMember -Group "Administrators" -Member "{{USER}}"Remove-LocalGroupMember -Group "Administrators" -Member "{{USER}}"
Réseau
powershell
Variables
{{SERVER_IP}}
{{TARGET}}
{{PORT}}
{{TARGET_DOMAIN}}
# Configuration réseauGet-NetIPAddressGet-NetIPConfigurationGet-NetAdapter# Configurer une IP statiqueNew-NetIPAddress -InterfaceAlias "Ethernet" -IPAddress "{{SERVER_IP}}" -PrefixLength 24 -DefaultGateway "192.168.1.1"Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses "8.8.8.8","8.8.4.4"# Connexions activesGet-NetTCPConnection -State ListenGet-NetTCPConnection | Where-Object {$_.State -eq "Established"}# Tests réseauTest-NetConnection -ComputerName {{TARGET}} -Port {{PORT}}Test-NetConnection google.com -TraceRouteResolve-DnsName {{TARGET_DOMAIN}}
Registre Windows
powershell
Variables
{{SERVICE}}
# Lire une cléGet-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion"Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\{{SERVICE}}"# Créer / modifier une valeurSet-ItemProperty -Path "HKLM:\SOFTWARE\MyApp" -Name "Debug" -Value 1New-Item -Path "HKLM:\SOFTWARE\MyApp"New-ItemProperty -Path "HKLM:\SOFTWARE\MyApp" -Name "Version" -Value "1.0" -PropertyType String# SupprimerRemove-ItemProperty -Path "HKLM:\SOFTWARE\MyApp" -Name "Debug"Remove-Item -Path "HKLM:\SOFTWARE\MyApp" -Recurse
Gestion des logs
powershell
# Journaux d'événementsGet-EventLog -LogName Application -Newest 50Get-EventLog -LogName Security -EntryType Error,Warning -Newest 20Get-EventLog -LogName System -Source "Service Control Manager"# Avec Get-WinEvent (plus puissant)Get-WinEvent -LogName Application -MaxEvents 100Get-WinEvent -FilterHashtable @{LogName='Security'; Id=4625} # Échecs de connexionGet-WinEvent -FilterHashtable @{LogName='Security'; Id=4624} # Connexions réussiesGet-WinEvent -FilterHashtable @{LogName='System'StartTime=(Get-Date).AddHours(-1)Level=2 # Error}
WMI / CIM — Informations système
powershell
# Infos systèmeGet-CimInstance -ClassName Win32_ComputerSystemGet-CimInstance -ClassName Win32_OperatingSystemGet-CimInstance -ClassName Win32_ProcessorGet-CimInstance -ClassName Win32_PhysicalMemory# DisquesGet-CimInstance -ClassName Win32_LogicalDisk | Select-Object DeviceID, Size, FreeSpaceGet-PSDrive -PSProvider FileSystem# Résumé mémoire$mem = Get-CimInstance -ClassName Win32_OperatingSystem[Math]::Round(($mem.TotalVisibleMemorySize - $mem.FreePhysicalMemory) / $mem.TotalVisibleMemorySize * 100, 1)
Exécution à distance
powershell
Variables
{{SERVER_IP}}
# Activer WinRMEnable-PSRemoting -Force# Session distante interactiveEnter-PSSession -ComputerName {{SERVER_IP}} -Credential (Get-Credential)# Commande distante one-shotInvoke-Command -ComputerName {{SERVER_IP}} -ScriptBlock { Get-Service } -Credential (Get-Credential)# Sur plusieurs serveurs$servers = @("srv01", "srv02", "srv03")Invoke-Command -ComputerName $servers -ScriptBlock { hostname; Get-Service | Where Status -eq "Stopped" }
💡 Tip —
Toujours utiliser ConvertTo-SecureString pour les mots de passe dans les scripts, jamais en clair. Pour les scripts automatisés, utiliser des credentials stockés dans le Windows Credential Manager ou un secret manager.
OPS·BRAIN v1.09 notes · SysAdminlocal