Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 668 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you comment out code in PowerShell?

#11
I'm a little bit late to this party but seems that nobody actually wrote all use cases. So...

Only supported version of PowerShell these days (*fall of 2020 and beyond*) are:

- Windows PowerShell 5.1.x
- PowerShell 7.0.x.

You don't want to or you shouldn't work with different versions of PowerShell.

**Both versions** (*or any another version which you could come around WPS 3.0-5.0, PS Core 6.x.x on some outdated stations*) **share the same comment functionality.**

## One line comments

```lang-none
# 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
```

## Multi line comments

```lang-none
<#
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.
#>
```

## Nested multi line comments

```lang-none
<#
Nope, these are not allowed in PowerShell.

<# This will break your first multiline comment block... #>
...and this will throw a syntax error.
#>
```

## In code nested multi line comments

```lang-none
<#
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
```

## Edge case scenario

```lang-none
# 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.
```



Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through