A function is a custom PowerShell code that has three principles:
Short: The function name is short and obvious.
Aggregations: Functions can complete multiple operations.
Encapsulation and extension: A batch of PowerShell statements are encapsulated to achieve new functional requirements.
The structure of a function consists of three parts: function name, parameter, function body
Copy Code code as follows:
Function FuncName (args[])
{
Code
}
Using functions as aliases
If PowerShell does not support the "CD ..." command, you can implement this function by defining a function:
Copy Code code as follows:
PS c:powershell> Function CD. {CD.}
PS c:powershell> CD.
PS c:>
If PowerShell does not support the ping command, you can do the same:
Copy Code code as follows:
PS c:powershell> Function Ping2 {PING. Exe-n 1 $args}
PS c:powershell> Ping2 www.mossfly.com
Ping mossfly.com [116.255.205.70] has 32 bytes of data:
The request timed out.
Ping Statistics for 116.255.205.70:
Packet: Sent = 1, received = 0, lost = 1 (100% lost),
Multi-line Input definition function on console
Copy Code code as follows:
PS c:powershell> Function myping
>> {
>> PING. Exe-n 1 $args
>>}
>>
PS c:powershell>
To streamline a function into one line
You can define a function on one line, but it's not easy to read and understand, and you can split each command with a semicolon.
Copy Code code as follows:
PS c:powershelltest> Function cd ... {CD ... cd ...}
PS c:powershelltest> CD ...
PS c:>
Using a text editor
Functions can be written on a text editor, and then copied into the PowerShell console. If the console is set to Quick edit mode, after copying from Notepad, the right mouse button on the console completes the paste.
Update function
If you want to update a function that has already been defined, the simple way is to redefine it so that the new definition overwrites the old definition. However, if the function code does not save a copy, you can first export the function definition to the PS file, and then you can edit it.
Copy Code code as follows:
PS c:powershell> $function: myping | Out-file MYPING.PS1
PS c:powershell> $function: myping
Ping. Exe-n 1 $args
PS c:powershell> $function: myping | Out-file MYPING.PS1
PS c:powershell>. Myping.ps1
You must specify an IP address.
PS c:powershell> notepad.exe $$
Delete function
The function defined by the console will only take effect at the current session and will automatically disappear once the console exits. Delete a defined function without shutting down the console, but use the virtual drive method:
Copy Code code as follows:
PS c:powershell> del function:myping
PS c:powershell> myping
The "myping" item cannot be recognized as a cmdlet, function, script file, or the name of a running program. Please check the name of the
Spelling, if the path is included, make sure the path is correct, and then try again.
Location: 1 Characters: 7
+ myping <<<<
+ Categoryinfo:objectnotfound: (myping:string) [], COMMANDNOTFO
Undexception
+ fullyqualifiederrorid:commandnotfoundexception
Suggestion [3,general]: The command myping was not found, but it does exist in the current location. Windows Pow
The shell does not load commands from the current location by default. If you trust this command, type ". Myping" instead. Yes
Please refer to "Get-help about_command_precedence" for more details.