Je suis un peu en retard à cette fête, mais il semble que personne n'ait réellement écrit tous les cas d'utilisation. Donc...
La seule version supportée de PowerShell ces jours-ci ( automne 2020 et au-delà ) sont :
- Windows PowerShell 5.1.x
- PowerShell 7.0.x.
Vous ne voulez pas ou ne devriez pas travailler avec différentes versions de PowerShell.
Les deux versions ( ou toute autre version que vous pourriez rencontrer WPS 3.0-5.0, PS Core 6.x.x sur certaines stations dépassées ) partagent la même fonctionnalité de commentaire.
Commentaires d'une ligne
# Get all Windows Service processes <-- one line comment, it starts with '#'
Get-Process -Name *host*
Get-Process -Name *host* ## You could put as many ### as you want, it does not matter
Get-Process -Name *host* # | Stop-Service # Everything from the first # until end of the line is treated as comment
Stop-Service -DisplayName Windows*Update # -WhatIf # You can use it to comment out cmdlet switches
Commentaires multi-lignes
<#
Everyting between '< #' and '# >' is
treated as a comment. A typical use case is for help, see below.
# You could also have a single line comment inside the multi line comment block.
# Or two... :)
#>
<#
.SYNOPSIS
A brief description of the function or script.
This keyword can be used only once in each topic.
.DESCRIPTION
A detailed description of the function or script.
This keyword can be used only once in each topic.
.NOTES
Some additional notes. This keyword can be used only once in each topic.
This keyword can be used only once in each topic.
.LINK
A link used when Get-Help with a switch -OnLine is used.
This keyword can be used only once in each topic.
.EXAMPLE
Example 1
You can use this keyword as many as you want.
.EXAMPLE
Example 2
You can use this keyword as many as you want.
#>
Commentaires imbriqués sur plusieurs lignes
<#
Nope, these are not allowed in PowerShell.
<# This will break your first multiline comment block... #>
...and this will throw a syntax error.
#>
Commentaires multilignes imbriqués dans le code
<#
The multi line comment opening/close
can be also used to comment some nested code
or as an explanation for multi chained operations..
#>
Get-Service | <# Step explanation #>
Where-Object { $_.Status -eq [ServiceProcess.ServiceControllerStatus]::Stopped } |
<# Format-Table -Property DisplayName, Status -AutoSize |#>
Out-File -FilePath Services.txt -Encoding Unicode
Scénario limite
# Some well written script
exit
Writing something after exit is possible but not recommended.
It isn't a comment.
Especially in Visual Studio Code, these words baffle PSScriptAnalyzer.
You could actively break your session in VS Code.
36 votes
Note '#' est un commentaire dans beaucoup de langages shell et script : bash, python, php, ruby, et maintenant powershell.
79 votes
C'est exactement pourquoi j'ai supposé que
#
n'est pas un commentaire dans un langage de script basé sur Windows ou Microsoft.4 votes
C'est parce que, apparemment, contrairement à toutes leurs autres technologies, MS n'a pas donné à powershell une référence décente. Je ne l'ai trouvé nulle part.
12 votes
@dudeNumber4 PowerShell possède l'une des références les plus complètes de tous les langages. Afin qu'une fonctionnalité soit ajoutée au langage, il faut doit comprennent une gamme complète
help
une documentation avec des exemples, des listes de méthodes et de membres, etc.