Defines what a script block is and explains how to use script blocks in (about_script_blocks)

   
# TOPIC
about_Script_Blocks

# SHORT DESCRIPTION
Defines what a script block is and explains how to use script blocks in
the Windows PowerShell programming language.

# LONG DESCRIPTION
In the Windows PowerShell programming language, a script block is a
collection of statements or expressions that can be used as a single unit.
A script block can accept arguments and return values.

Syntactically, a script block is a statement list in braces, as shown in
the following syntax:

{<statement list>}

A script block returns the output of all the commands in the script block,
either as a single object or as an array.

Like functions, a script block can include parameters. Use the Param
keyword to assign named parameters, as shown in the following syntax:

{
param ([type]$parameter1 [,[type]$parameter2])
<statement list>
}

In a script block, unlike a function, you cannot specify parameters outside
the braces.

Like functions, script blocks can include the DynamicParam, Begin, Process,
and End keywords. For more information, see about_Functions and
about_Functions_Advanced.

Using Script Blocks

A script block is an instance of a Microsoft .NET Framework type
(System.Management.Automation.ScriptBlock). Commands can have script
block parameter values. For example, the Invoke-Command cmdlet has a
ScriptBlock parameter that takes a script block value, as shown in this
example:

C:\PS> invoke-command -scriptblock { get-process }
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
999 28 39100 45020 262 15.88 1844 communicator
721 28 32696 36536 222 20.84 4028 explorer
. . .

The script block that is used as a value can be more complicated, as
shown in the following example:

C:\PS> invoke-command -scriptblock { param ($uu = "Parameter");
"$uu assigned." }
Parameter assigned.

The script block in the preceding example uses the Param keyword to
create a parameter that has a default value. The following example uses
the Args parameter of the Invoke-Command cmdlet to assign a different
value to the parameter:

C:\PS> invoke-command -scriptblock {param ($uu = "Parameter");
"$uu assigned."} -args "Other value"
Other value assigned.

You can assign a script block to a variable, as shown in the following
example:

C:\PS> $a = {param ($uu = "Parameter"); "$uu assigned."}

You can use the variable with a cmdlet such as Invoke-Command, as shown
in the following example:

C:\PS> invoke-command -scriptblock $a -args "Other value"
Other value assigned.

You can run a script block that is assigned to a variable by using the
call operator (&), as shown in the following example:

C:\PS> &$a
Parameter assigned.

You can also provide a parameter to the script block, as shown in the
following example:

C:\PS> &$a "Other value"
Other value assigned.

If you want to assign the value that is created by a script block to a
variable, use the call operator to run the script block directly, as
shown in the following example:

C:\PS> $a = &{param ($uu = "Parameter"); "$uu assigned."}
C:\PS> $a
Parameter assigned.

For more information about the call operator, see about_Operators.

SEE ALSO
about_Functions
about_Functions_Advanced
about_Operators

C:\Windows>powershell get-help about_script_internationalization -full

ColorConsole [Version 1.7.1000] PowerShell 2.0-Export
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2014 Microsoft Corporation.

OS: Windows 10, 8.1, 8, 7, Vista / Windows Server 2016, 2012, 2008
»»»» ColorConsole





... Home  
... Impressum
... ColorConsole

... CMD
... Netsh-XP
... Netsh-7
... Netsh-Vista
... Powershell










Download the Windows 10 Starter, Home (Basic Premium), Professional und Ultimate!

... facebook.de
HTTP: ... PS_Windows/en/about_script_blocks.htm
0.062

System requirements for Windows 8 and 8.1?

 /

Wie kann ich mit Q-Dir Bilder und Poster drucken?

 /

Enable or Disable Data Execution Prevention in Windows 8.1, 8 and 10 (turn-off/-on)!

 /

Microsoft Windows-Tool zum Entfernen bösartiger Software manuell starten!

 /