PowerShell n’est pas un simple interpréteur de commandes textuelles. Chaque commande retourne des objets complets, contenant des propriétés et des méthodes. Pour comprendre ce que l’on peut faire avec un objet, l’outil essentiel est Get-Member.
Get-Member (alias gm) analyse ce que PowerShell reçoit en entrée et affiche tous les « membres » disponibles :
Quand tu tapes une commande comme :
Get-ChildItem
PowerShell ne renvoie pas du texte mais des objets de type System.IO.FileInfo ou System.IO.DirectoryInfo. Pour savoir ce que tu peux exploiter dans ces objets (nom, taille, extension, date, méthodes utiles…), tu utilises :
Get-ChildItem | Get-Member
<commande> | Get-Member
ou plus court :
<commande> | gm
On ne montre ici que les propriété et méthodes principales.
Get-Item "C:\Test\demo.txt" | Get-Member
TypeName : System.IO.FileInfo
Name MemberType Definition
---- ---------- ----------
AppendText Method System.IO.StreamWriter AppendText()
CopyTo Method System.IO.FileInfo CopyTo(string destFileName), System.IO.FileInfo CopyTo(string destFileName, bool overwrite)
Delete Method void Delete()
GetType Method type GetType()
MoveTo Method void MoveTo(string destFileName)
PSChildName NoteProperty string PSChildName=unzip.txt
PSDrive NoteProperty PSDriveInfo PSDrive=D
PSIsContainer NoteProperty bool PSIsContainer=False
PSParentPath NoteProperty string PSParentPath=Microsoft.PowerShell.Core\FileSystem::D:\Projects\cepes\site-cours
PSPath NoteProperty string PSPath=Microsoft.PowerShell.Core\FileSystem::D:\Projects\cepes\site-cours\unzip.txt
Attributes Property System.IO.FileAttributes Attributes {get;set;}
CreationTime Property datetime CreationTime {get;set;}
Directory Property System.IO.DirectoryInfo Directory {get;}
DirectoryName Property string DirectoryName {get;}
Exists Property bool Exists {get;}
Extension Property string Extension {get;}
FullName Property string FullName {get;}
IsReadOnly Property bool IsReadOnly {get;set;}
Length Property long Length {get;}
Name Property string Name {get;}
BaseName ScriptProperty System.Object BaseName {get=if ($this.Extension.Length -gt 0){$this.Name.Remove($this.Name.Length - $this.Extension.Length)}else{$this.Name};}
VersionInfo ScriptProperty System.Object VersionInfo {get=[System.Diagnostics.FileVersionInfo]::GetVersionInfo($this.FullName);}
Tu verras apparaître des propriétés comme :
Et des méthodes comme :
Ce sont elles que tu vas pouvoir exploiter dans la suite de tes scripts.
Get-ChildItem | gm
Résultat : tu observes que la commande retourne des objets différents selon le type :
D’où l’importance de toujours vérifier ce que PowerShell manipule réellement.
Get-ChildItem | gm -MemberType Property
Get-ChildItem | gm -MemberType Method
Dans la sortie de Get-Member, la première ligne est cruciale :
TypeName: System.IO.FileInfo
Cela indique de quel type d’objet il s’agit.
C’est la clé pour comprendre ce que tu peux en faire, et pour savoir où chercher dans la documentation .NET.
Get-ChildItem -File | Measure-Object -Property Length -Sum
Comment sait-on que la propriété Length existe ?
Grâce à Get-Member.
Si Get-Member t’affiche trop de lignes, commence par examiner les propriétés les plus utiles :
Get-ChildItem | gm -MemberType Property | Select-Object Name
Cela liste uniquement le nom des propriétés pour éviter l’overdose d’informations.
Get-Member est l’outil fondamental pour comprendre tout ce que PowerShell manipule.
Sans lui, on travaille "à l'aveugle".
Avec lui, on maîtrise :
C’est un réflexe à acquérir dès les premiers cours.