Introduction
Advanced functions let you create powerful scripts that behave like built-in commands with extra features.
function FunctionName { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string]$Param1 ) # Function code here }
function Get-Greeting { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string]$Name ) Write-Output "Hello, $Name!" }
function Test-Verbose { [CmdletBinding()] param() Write-Verbose "This is a verbose message." Write-Output "Done." }
function Get-Data { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [int]$Id ) if ($Id -lt 0) { Write-Error "Id must be positive." return } Write-Output "Data for Id $Id" }
function Show-Info { [CmdletBinding()] param( [Parameter(Mandatory=$true, HelpMessage="Enter your name.")] [string]$Name ) Write-Verbose "Verbose mode is on." Write-Output "Hello, $Name!" } # Call the function with verbose Show-Info -Name "Alice" -Verbose
[CmdletBinding()] in a PowerShell function?Name?Get-Greeting -Name 'Alice'?
function Get-Greeting {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$Name
)
Write-Output "Hello, $Name!"
}function Test-Function {
param(
[Parameter(Mandatory=$true)]
[string]$InputValue
)
[CmdletBinding()]
Write-Output $InputValue
}-Verbose to show extra messages only when the switch is used. Which code snippet correctly implements this behavior?Pass the test and this pattern moves unknown