This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| informatique:windows:powershell [2022/02/10 09:17] – yahiko | informatique:windows:powershell [2025/08/12 13:20] (current) – yahiko | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| ===== Autoriser l' | ===== Autoriser l' | ||
| - | <codedoc | + | <code powershell> |
| set-executionpolicy remotesigned | set-executionpolicy remotesigned | ||
| - | </codedoc> | + | </code> |
| ===== Lancer un script Powershell à partir d'un Batch ===== | ===== Lancer un script Powershell à partir d'un Batch ===== | ||
| - | <codedoc | + | <code code: |
| Powershell.exe -executionpolicy remotesigned -File NOMDUSCRIPT.ps1 | Powershell.exe -executionpolicy remotesigned -File NOMDUSCRIPT.ps1 | ||
| - | </codedoc> | + | </code> |
| + | |||
| + | ===== 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 ===== | ===== Tester la présence d'un dossier ===== | ||
| - | <codedoc | + | <code powershell> |
| $dossier = " | $dossier = " | ||
| Line 22: | Line 29: | ||
| mkdir $dossier | mkdir $dossier | ||
| } | } | ||
| - | </codedoc> | + | </code> |
| ===== Télécharger un fichier ===== | ===== Télécharger un fichier ===== | ||
| - | <codedoc | + | <code powershell> |
| $url = http:// | $url = http:// | ||
| Invoke-WebRequest -Uri $url -OutFile C: | Invoke-WebRequest -Uri $url -OutFile C: | ||
| - | </codedoc> | + | </code> |
| ===== Créer un raccourci ===== | ===== Créer un raccourci ===== | ||
| - | <codedoc | + | <code powershell> |
| $WshShell = New-Object -ComObject WScript.Shell | $WshShell = New-Object -ComObject WScript.Shell | ||
| $Shortcut = $WshShell.CreateShortcut(" | $Shortcut = $WshShell.CreateShortcut(" | ||
| $Shortcut.TargetPath = " | $Shortcut.TargetPath = " | ||
| $Shortcut.Save() | $Shortcut.Save() | ||
| - | </codedoc> | + | </code> |
| ===== Créer plusieurs comptes / activer ===== | ===== Créer plusieurs comptes / activer ===== | ||
| - | <codedoc | + | <code powershell> |
| # On met les comptes dans un tableau | # On met les comptes dans un tableau | ||
| $comptes = @(" | $comptes = @(" | ||
| Line 63: | Line 70: | ||
| } | } | ||
| Set-LocalUser -Name ' | Set-LocalUser -Name ' | ||
| - | </codedoc> | + | </code> |
| ===== Désactiver et activer des trucs ===== | ===== Désactiver et activer des trucs ===== | ||
| - | <codedoc | + | <code powershell> |
| # Désactiver UAC | # Désactiver UAC | ||
| New-ItemProperty -Path HKLM: | New-ItemProperty -Path HKLM: | ||
| Line 87: | Line 94: | ||
| # Installation de .NET3.5 | # Installation de .NET3.5 | ||
| Enable-WindowsOptionalFeature -Online -FeatureName " | Enable-WindowsOptionalFeature -Online -FeatureName " | ||
| - | </codedoc> | + | </code> |
| ===== Intégrer un poste à un domaine ===== | ===== Intégrer un poste à un domaine ===== | ||
| - | <codedoc | + | <code powershell> |
| Add-Computer -DomainName DOMAINE.LOCAL -Credential DOMAINE\administrateur -Restart -Force | Add-Computer -DomainName DOMAINE.LOCAL -Credential DOMAINE\administrateur -Restart -Force | ||
| - | </codedoc> | + | </code> |
| + | |||
| + | ===== 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 "'","" | ||
| + | </ | ||
| + | |||
| + | ===== Groupes AD ===== | ||
| + | ==== Lister les groupes d'une OU ==== | ||
| + | <code powershell> | ||
| + | Get-AdGroup -filter * -searchbase " | ||
| + | </ | ||
| + | |||
| + | ==== Lister les utilisateurs d'un groupe ==== | ||
| + | <code powershell> | ||
| + | Get-ADGroupMember -Identity " | ||
| + | </ | ||
| + | |||
| + | ===== Vérifier les droits NTFS d'un dossier ===== | ||
| + | |||
| + | Non récursif. | ||
| + | |||
| + | <code powershell> | ||
| + | Get-NTFSAccess ' | ||
| + | |||
| + | Path: D: | ||
| + | |||
| + | |||
| + | Account | ||
| + | ------- | ||
| + | BUILTIN\Administrateurs | ||
| + | BUILTIN\Administrateurs | ||
| + | AUTORITE NT\Système | ||
| + | CREATEUR PROPRIETAIRE | ||
| + | BUILTIN\Utilisateurs | ||
| + | BUILTIN\Utilisateurs | ||
| + | BUILTIN\Utilisateurs | ||
| + | </ | ||
| + | |||
| + | ===== Lister les utilisateurs connectés en RDP ===== | ||
| + | |||
| + | <code powershell> | ||
| + | Get-CimInstance -ClassName Win32_UserProfile | where {-not $_.Special} | select LocalPath, LastUseTime, | ||
| + | </ | ||
| + | |||
| + | Source : https:// | ||
| + | ===== Uptime ===== | ||
| + | <code powershell> | ||
| + | (get-date) – (gcim Win32_OperatingSystem).LastBootUpTime | ||
| + | </ | ||