This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
informatique:windows:powershell [2020/02/28 09:42] – external edit 127.0.0.1 | informatique:windows:powershell [2025/01/23 14:58] (current) – yahiko | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Powershell ====== | ====== Powershell ====== | ||
[[informatique: | [[informatique: | ||
+ | |||
+ | ===== Autoriser l' | ||
+ | <code powershell> | ||
+ | set-executionpolicy remotesigned | ||
+ | </ | ||
+ | |||
+ | ===== Lancer un script Powershell à partir d'un Batch ===== | ||
+ | <code code: | ||
+ | Powershell.exe -executionpolicy remotesigned -File NOMDUSCRIPT.ps1 | ||
+ | </ | ||
+ | |||
+ | ===== Lancer un script Powershell à partir d'un chemin UNC ===== | ||
+ | Attention, c'est très sale : | ||
+ | <code batch> | ||
+ | Powershell.exe -NoProfile -ExecutionPolicy bypass -File " | ||
+ | </ | ||
+ | Source : [[https:// | ||
+ | |||
+ | ===== Tester la présence d'un dossier ===== | ||
+ | <code powershell> | ||
+ | $dossier = " | ||
+ | |||
+ | if (Test-Path -Path $dossier) { | ||
+ | "Le dossier $dossier !" | ||
+ | } else { | ||
+ | " | ||
+ | mkdir $dossier | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Télécharger un fichier ===== | ||
+ | <code powershell> | ||
+ | $url = http:// | ||
+ | |||
+ | Invoke-WebRequest -Uri $url -OutFile C: | ||
+ | </ | ||
+ | |||
+ | ===== Créer un raccourci ===== | ||
+ | <code powershell> | ||
+ | $WshShell = New-Object -ComObject WScript.Shell | ||
+ | $Shortcut = $WshShell.CreateShortcut(" | ||
+ | $Shortcut.TargetPath = " | ||
+ | $Shortcut.Save() | ||
+ | </ | ||
+ | |||
+ | ===== Créer plusieurs comptes / activer ===== | ||
+ | <code powershell> | ||
+ | # On met les comptes dans un tableau | ||
+ | $comptes = @(" | ||
+ | ForEach ($compte in $comptes) | ||
+ | { | ||
+ | $op = Get-LocalUser | Where-Object {$_.Name -eq $compte} | ||
+ | if ( -not $op) { | ||
+ | " | ||
+ | $password = Read-Host " | ||
+ | New-LocalUser -Name $compte -Password $password -AccountNeverExpires -UserMayNotChangePassword | ||
+ | Add-LocalGroupMember -Group " | ||
+ | } else { | ||
+ | "Le compte $compte existe déjà" | ||
+ | if ((Get-LocalUser $compte).Enabled) { | ||
+ | "Le compte $compte est déjà activé" | ||
+ | } else { | ||
+ | " | ||
+ | Enable-LocalUser -Name $compte | ||
+ | "Le compte $compte est activé" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | Set-LocalUser -Name ' | ||
+ | </ | ||
+ | |||
+ | ===== Désactiver et activer des trucs ===== | ||
+ | <code powershell> | ||
+ | # Désactiver UAC | ||
+ | New-ItemProperty -Path HKLM: | ||
+ | |||
+ | # Désactiver pare-feu sur tous les profils | ||
+ | Set-NetFirewallProfile -Profile * -Enabled False | ||
+ | |||
+ | # Désactivation smart-screen | ||
+ | Set-ItemProperty -Path " | ||
+ | Set-ItemProperty -Path " | ||
+ | |||
+ | # Désactivation OOBE | ||
+ | Set-ItemProperty -Path " | ||
+ | Set-ItemProperty -Path " | ||
+ | Set-ItemProperty -Path " | ||
+ | |||
+ | # Activation client SMBv1 | ||
+ | Enable-WindowsOptionalFeature -Online -FeatureName " | ||
+ | |||
+ | # Installation de .NET3.5 | ||
+ | Enable-WindowsOptionalFeature -Online -FeatureName " | ||
+ | </ | ||
+ | |||
+ | ===== Intégrer un poste à un domaine ===== | ||
+ | <code powershell> | ||
+ | Add-Computer -DomainName DOMAINE.LOCAL -Credential DOMAINE\administrateur -Restart -Force | ||
+ | </ | ||
+ | |||
+ | ===== Récupérer le GUID des volumes ===== | ||
+ | <code powershell> | ||
+ | GWMI -namespace root\cimv2 -class win32_volume | FL -property DriveLetter, | ||
+ | </ | ||
+ | |||
+ | ===== Exporter les utilisateurs d'un AD ===== | ||
+ | <code powershell> | ||
+ | Get-ADUser -Filter * -Proerties * -SearchBase " | ||
+ | </ | ||
+ | |||
+ | ===== Changer la casse d'une variable ===== | ||
+ | |||
+ | <code powershell> | ||
+ | # En majuscule | ||
+ | CHAINE.ToUpper() | ||
+ | |||
+ | # En minuscule | ||
+ | CHAINE.ToLower() | ||
+ | </ | ||
+ | |||
+ | ===== Remplacer un caractère ou un espace ===== | ||
+ | |||
+ | C'est pas beau. | ||
+ | |||
+ | <code powershell> | ||
+ | # Un espace | ||
+ | $string = $string -replace " ","" | ||
+ | |||
+ | # Un apostrophe | ||
+ | $string = $string -replace "'","" | ||
+ | </ | ||
+ | |||
+ | ===== Lister les membres d'un groupe AD ===== | ||
+ | <code powershell> | ||
+ | Get-ADGroupMember -Identity " | ||
+ | </ | ||