Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ foreach ($file in Get-ChildItem)
```

In this example, the `foreach` loop uses a property of the `$file` variable to
perform a comparison operation (`$file.length -gt 100KB`). The `$file` variable
perform a comparison operation (`$file.Length -gt 100KB`). The `$file` variable
has all the properties of the object returned by the `Get-ChildItem`.

In the next example, the script displays the length and the last access time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ digital signature. For more information, see

The following sample creates a `Format-Table` custom view for the
**System.IO.DirectoryInfo** and **System.IO.FileInfo** objects created by
`Get-ChildItem`. The custom view is named **mygciview** and adds the
`Get-ChildItem`. The custom view is named **MyGciView** and adds the
**CreationTime** column to the table.

The custom view is created from an edited version of the
Expand Down Expand Up @@ -500,7 +500,7 @@ Update-FormatData -PrependPath $PSHOME\Format\MyFileSystem.Format.ps1xml
</Controls>
<ViewDefinitions>
<View>
<Name>mygciview</Name>
<Name>MyGciView</Name>
<ViewSelectedBy>
<SelectionSetName>FileSystemTypes</SelectionSetName>
</ViewSelectedBy>
Expand Down Expand Up @@ -541,14 +541,14 @@ Update-FormatData -PrependPath $PSHOME\Format\MyFileSystem.Format.ps1xml
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
[String]::Format("{0,10} {1,8}",
[string]::Format("{0,10} {1,8}",
$_.LastWriteTime.ToString("d"),
$_.LastWriteTime.ToString("t"))
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
[String]::Format("{0,10} {1,8}",
[string]::Format("{0,10} {1,8}",
$_.CreationTime.ToString("d"),
$_.LastWriteTime.ToString("t"))
</ScriptBlock>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,21 @@ You can retrieve a function's definition by accessing the **Definition**
property, as shown below.

```powershell
(Get-Item -Path function:more).Definition
(Get-Item -Path Function:more).Definition
```

You can also retrieve a function's definition using its provider path prefixed
by the dollar sign (`$`).

```powershell
$function:more
$Function:more
```

To retrieve the definition for a function that has a dash (`-`) in the name,
wrap the value after the dollar sign in curly braces.

```powershell
${function:Clear-Host}
${Function:Clear-Host}
```

### Getting selected functions
Expand All @@ -132,7 +132,7 @@ Get-Item -Path man | Format-Table -Wrap -AutoSize

### Working with Function provider paths

These commands both get the function named `c:`. The first command can be used
These commands both get the function named `C:`. The first command can be used
in any drive. The second command is used in the `Function:` drive. Because the
name ends in a colon, which is the syntax for a drive, you must qualify the
path with the drive name. Within the `Function:` drive, you can use either
Expand Down Expand Up @@ -160,7 +160,7 @@ function, it is available only in the scope in which it was created. To make a
function available, use a scope modifier when you create the function. For more
information, see [about_Scopes][15].

The following example uses the `global:` scope modifier to create a function in
The following example uses the `Global:` scope modifier to create a function in
the global scope.

```powershell
Expand All @@ -170,14 +170,14 @@ function New-Function {
[scriptblock] $Script
)

$lp = "Function:\global:$($name)"
Set-Item -LiteralPath $lp -Value $script -PassThru -Force
$lp = "Function:\Global:$($Name)"
Set-Item -LiteralPath $lp -Value $Script -PassThru -Force
}

New-Function -Name 'Win32:' -Script { Set-Location C:\Windows\System32 }
```

Without the `global:` scope modifier, the function would be created in the
Without the `Global:` scope modifier, the function would be created in the
local scope. When `New-Function` exits the newly created function would no
longer exist.

Expand Down Expand Up @@ -269,7 +269,7 @@ Get-Help Get-ChildItem
```

```powershell
Get-Help Get-ChildItem -Path function:
Get-Help Get-ChildItem -Path Function:
```

## See also
Expand Down
44 changes: 22 additions & 22 deletions reference/5.1/Microsoft.PowerShell.Core/About/about_Functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ run as if you had typed them at the command prompt.
Functions can be as simple as:

```powershell
function Get-PowerShellProcess { Get-Process PowerShell }
function Get-PowerShellProcess { Get-Process powershell }
```

Once a function is defined, you can use it like the built-in cmdlets. For
Expand Down Expand Up @@ -71,7 +71,7 @@ like a cmdlet without using `C#` programming. For more information, see
The following are the syntax for a function:

```Syntax
function [<scope:>]<name> [([type]$parameter1[,[type]$parameter2])]
function [<scope:>]<name> [([type]$Parameter1[,[type]$Parameter2])]
{
begin {<statement list>}
process {<statement list>}
Expand All @@ -82,7 +82,7 @@ function [<scope:>]<name> [([type]$parameter1[,[type]$parameter2])]
```Syntax
function [<scope:>]<name>
{
param([type]$parameter1 [,[type]$parameter2])
param([type]$Parameter1 [,[type]$Parameter2])
dynamicparam {<statement list>}
begin {<statement list>}
process {<statement list>}
Expand Down Expand Up @@ -120,7 +120,7 @@ processing, and an `end` block for one-time post-processing.
Function Test-ScriptCmdlet
{
[CmdletBinding(SupportsShouldProcess=$true)]
Param ($Parameter1)
param ($Parameter1)
begin{}
process{}
end{}
Expand Down Expand Up @@ -188,11 +188,11 @@ For example, the following function finds all `.jpg` files in the current
user's directories that were changed after the start date.

```powershell
function Get-NewPix
function Get-NewPicture
{
$start = Get-Date -Month 1 -Day 1 -Year 2010
$allpix = Get-ChildItem -Path $env:UserProfile\*.jpg -Recurse
$allpix | Where-Object {$_.LastWriteTime -gt $Start}
$allPics = Get-ChildItem -Path $Env:USERPROFILE\*.jpg -Recurse
$allPics | Where-Object {$_.LastWriteTime -gt $Start}
}
```

Expand Down Expand Up @@ -234,25 +234,25 @@ in the following sample syntax:

```Syntax
function <name> {
param ([type]$parameter1 [,[type]$parameter2])
param ([type]$Parameter1 [,[type]$Parameter2])
<statement list>
}
```

You can also define parameters outside the braces without the `Param` keyword,
You can also define parameters outside the braces without the `param` keyword,
as shown in the following sample syntax:

```Syntax
function <name> [([type]$parameter1[,[type]$parameter2])] {
function <name> [([type]$Parameter1[,[type]$Parameter2])] {
<statement list>
}
```

Below is an example of this alternative syntax.

```powershell
function Add-Numbers([int]$one, [int]$two) {
$one + $two
function Add-Numbers([int]$One, [int]$Two) {
$One + $Two
}
```

Expand All @@ -269,7 +269,7 @@ the value of the `$Size` parameter, and it excludes directories:

```powershell
function Get-SmallFiles {
Param($Size)
param ($Size)
Get-ChildItem $HOME | Where-Object {
$_.Length -lt $Size -and !$_.PSIsContainer
}
Expand Down Expand Up @@ -369,8 +369,8 @@ name, as shown in the following example:

```powershell
function Switch-Item {
param ([switch]$on)
if ($on) { "Switch on" }
param ([switch]$On)
if ($On) { "Switch on" }
else { "Switch off" }
}
```
Expand All @@ -379,7 +379,7 @@ When you type the `On` switch parameter after the function name, the function
displays `Switch on`. Without the switch parameter, it displays `Switch off`.

```powershell
Switch-Item -on
Switch-Item -On
```

```Output
Expand All @@ -398,15 +398,15 @@ You can also assign a **Boolean** value to a switch when you run the function,
as shown in the following example:

```powershell
Switch-Item -on:$true
Switch-Item -On:$true
```

```Output
Switch on
```

```powershell
Switch-Item -on:$false
Switch-Item -On:$false
```

```Output
Expand Down Expand Up @@ -607,7 +607,7 @@ You can specify the scope of a function. For example, the function is added to
the global scope in the following example:

```powershell
function global:Get-DependentSvs {
function Global:Get-DependentSvs {
Get-Service | Where-Object {$_.DependentServices}
}
```
Expand All @@ -633,21 +633,21 @@ The following command displays all the functions in the current session of
PowerShell:

```powershell
Get-ChildItem function:
Get-ChildItem Function:
```

The commands in the function are stored as a script block in the definition
property of the function. For example, to display the commands in the Help
function that comes with PowerShell, type:

```powershell
(Get-ChildItem function:help).Definition
(Get-ChildItem Function:help).Definition
```

You can also use the following syntax.

```powershell
$function:help
$Function:help
```

For more information about the `Function:` drive, see the help topic for the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function.
This method is called to request confirmation from the user before the function
performs an action that would change the system. The function can continue
based on the Boolean value returned by the method. This method can only be
called only from within the `Process{}` block of the function. The
called only from within the `process {}` block of the function. The
`CmdletBinding` attribute must also declare that the function supports
`ShouldProcess` (as shown in the previous example).

Expand All @@ -160,7 +160,7 @@ Functions can call two different methods when errors occur. When a
non-terminating error occurs, the function should call the `WriteError` method,
which is described in the `Write` methods section. When a terminating error
occurs and the function can't continue, it should call the
`ThrowTerminatingError` method. You can also use the `Throw` statement for
`ThrowTerminatingError` method. You can also use the `throw` statement for
terminating errors and the [Write-Error][22] cmdlet for non-terminating errors.

For more information, see
Expand Down
Loading