# search for a moduleFind-Module <ModuleName>
# install / uninstall a moduleInstall-Module-Name <ModuleName> -Scope CurrentUser
Uninstall-Module-Name <ModuleName>
# list loaded modulesGet-Module# list installed modulesGet-Module-ListAvailable# load / unload a moduleImport-Module <ModuleName>
Remove-Module <ModuleName>
# Installer un module dans une console ouverte en mode administrateurInstall-Module-Name <ModuleName>
Uninstall-Module-Name <ModuleName>
Find-Module <ModuleName>
# Searching for commands with up/down arrow is really handy. The# option "moves to end" is useful if you want the cursor at the end# of the line while cycling through history like it does w/o searching,# without that option, the cursor will remain at the position it was# when you used up arrow, which can be useful if you forget the exact# string you started the search on.Set-PSReadLineOption-HistorySearchCursorMovesToEndSet-PSReadLineKeyHandler-Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler-Key DownArrow -Function HistorySearchForward
Provides colored version of the Get-ChildItem Cmdlet.
Install-Module-AllowClobberGet-ChildItemColor-Scope CurrentUser
# -AllowClobber option is needed so Get-ChildItemColor may override the existing command Out-Default.
If (-Not (Test-Path Variable:PSise)) { # Only run this in the console and not in the ISEImport-ModuleGet-ChildItemColorSet-Alias l Get-ChildItemColorFormatWide-option AllScope
Set-Alias ll Get-ChildItem-option AllScope
}
winget install gerardog.gsudo
choco install gsudo
# elevate the current shell in the current console window (Cmd/PowerShell/Pwsh Core/Yori)
sudo
# launch the current shell elevated in a new console window
sudo -n# command
sudo md"C:\Windows\Test"# Enable credentials cache (less UAC popups)
sudo config CacheMode Auto
L'installation du module via PsGet Install-Module PoShWarp ne fonctionne pas. Il faut l'installer mauellement.
Télécharger PoShWarp.psm1 et le copier dans $home\Documents\WindowsPowerShell\Modules\PoShWarp
Le module sera importer automatiquement au démarrage de powershell.
Shift + clique droit → Open PowerShell window here
Autre solution: créer sa propre entrée dans le menu contextuel d'Explorer regedit → Computer\HKEY_CLASSES_ROOT\Directory\Background\shell
Il n'est malheureusement pas possible de supprimer la String-Value Extended du dossier Powershell pour rendre l'entrée Open PowerShell window here accessible sans avoir à appuyer sur Shift.
# Get the ID and security principal of the current user account$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
# Get the security principal for the Administrator role$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if we are currently running "as Administrator"if ($myWindowsPrincipal.IsInRole($adminRole))
{
# We are running "as Administrator" - so change the title and background color to indicate this$Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"$Host.UI.RawUI.BackgroundColor = "DarkBlue"clear-host
}
else
{
# We are not running "as Administrator" - so relaunch as administrator# Create a new process object that starts PowerShell$newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
# Specify the current script path and name as a parameter$newProcess.Arguments = $myInvocation.MyCommand.Definition;
# Indicate that the process should be elevated$newProcess.Verb = "runas";
# Start the new process
[System.Diagnostics.Process]::Start($newProcess);
# Exit from the current, unelevated, processexit
}
# Code à exécuter avec les droits UAC# Permet de garder la console ouverteWrite-Host-NoNewLine"`nPress any key to continue..."$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
# ligne 17 remplacer « TabExpansion » par « TabExpansion2 »$OldTabExpansion = Get-Content Function:TabExpansion2
Alias
Les alias servent juste à lier une commande à un exécutable ou à une autre commande.
Pour lier une commande à du code utiliser plutôt une fonction.
# liste des aliasget-aliasgal# connaitre la valeur d'un alias
alias <mon-alias>
# créé l'alias np pour Notepad++New-Alias np "C:\Program Files\Notepad++\notepad++.exe"
Pour rendre les alias permanents il faut les ajouter au profil.
Set-Alias pour modifier un alias, New-Alias pour créer un nouvel alias, créé une erreur en cas de doublon.
Cannot resolve alias 'xxx' because it refers to term 'yyy'
Cannot resolve alias 'xxx' because it refers to term 'yyy', which is not recognized as a cmdlet, function, operable program, or script file
# commande de création du profil : création du dossier et du fichiernew-item-path$profile-itemtype file -force# l'option force permet d'écraser le profil s'il existait déjàecho$profile# $home\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
# include other files
. $home\Documents\WindowsPowerShell\Alias.ps1
. $home\Documents\WindowsPowerShell\Function.ps1
Alias.ps1
New-Alias l 'Get-ChildItem'New-Alias e 'C:\WINDOWS\explorer.exe'New-Alias np 'C:\Program Files\Notepad++\notepad++.exe'New-Alias aaa "$home\Documents\Scripts\MyScript.ps1"
Pour modifier les couleurs et la taille de la fenêtre, il est plus simple de modifier les propriétés de la fenêtre.
Propriétés de la fenêtre
PowerShell
Cmd
Quick Edit Mode
Oui
Non
Font
Raster Fonts 10x20
Raster Fonts 10x20
Screen
120x3000
80x300
Window
120x50
80x25
Couleur de fond
1, 36, 86
0, 0, 0
Couleur du texte
238, 237, 240
192, 192, 192
Changer la police pour Consolas 14Lucida Console 14
Couleurs
# lister et afficher les couleurs
[enum]::GetValues([System.ConsoleColor]) | Foreach-Object {Write-Host$_-ForegroundColor$_}
Utiliser l'outil Windows Console Colortool pour changer la palette des couleurs.
Il modifie les clés ColorTableXX dans le registre HKEY_CURRENT_USER\Console
# afficher la table des couleurs courantes
colortool.exe -c# charger un thème sans modifier la configuration
colortool.exe [scheme]
# modifier le thème par défaut sans modifier le terminal courant
colortool.exe -d [scheme]
# charger un thème et modifier le thème par défaut
colortool.exe -b [scheme]
# retour au thème par défaut
colortool.exe -b cmd-legacy
colortool chercher les thèmes dans le sous-dossier schemes. Il est aussi possible de spécifier le chemin complet vers un thème.
Autres thèmes: iTerm2-Color-Schemes (DarkSide, OneHalfDark)
DarkMagenta (6ème couleur en partant de la gauche) en bleu (1,36,86) pour son background
DarkYellow (7ème couleur en partant de la gauche) en gris clair (238,237,240) pour son foreground
Remodifier ces couleurs pour un rendu correspondant au thème:
clique-droit dans le titre de la fenêtre → Properties
Screen Background: 1ère couleur en partant de la gauche
Screen Text: 1ère couleur en partant de la droite
Raccourcis
Les raccourcis vers powershell contiennent une configuration de la palette des couleurs et est prioritaire sur celle du registre.
Les nouveaux raccourci créé prennent la configuration de la palette de couleurs du registre.
Remplacer les raccourcis dans %AppData%\Microsoft\Windows\Start Menu\Programs\Windows PowerShell par des nouveaux.
Il est donc possible de créer un raccourci pour chaque palette de couleurs.
# import du module dans le profileImport-Module"Oh-My-Posh"-DisableNameChecking-NoClobber
$env:USERPROFILE\.oh-my-posh.config.ps1
# copy the template configuration from template\oh-my-posh.config.ps1$plugins= "git", "ls", "ll", "psutils", "psreadline"# psreadline: historique à la zsh# ls: ls à la bash# ll: Get-ChildItem$theme = "base"
Pour psreadline, télécharger la version nightly build.
Puis remplacer le dossier C:\Program Files\WindowsPowerShell\Modules\PSReadline\2.0.0
File cannot be loaded because the execution of scripts is disabled on this system
Par défaut les scripts PS ne sont pas autorisé à s'exécuter : « Impossible de charger le fichier ..., car l'exécution de scripts est désactivée sur ce système. »
# get the execution policy for each scopeGet-ExecutionPolicy-List# change the execution policy for the current user to RemoteSignedSet-ExecutionPolicy RemoteSigned -Scope CurrentUser