Scripting
Cmdlets
Cmdlets are the heart-and-soul of Windows Powershell. Cmdlets include everything from reading and writing text files, to managing event logs, to sorting and filtering data. Unlike traditional command-line interface commands, Windows Powershell Cmdlets are designed to deal with objects - structured information that is more than just a string of characters appearing on the screen. Powershell Cmdlets use the Verb-Noun pairs. The Verb part of the name identifies the action the Cmdlet will perform.
Key built-in Cmdlets
Get-Help
→ Retrieves a list of all available Powershell Cmdlets.
Get-Command
→ Retrieves a list of all available Powershell Cmdlets.
Get-ChildItem
→ Gets the files and folders in a file system drive (has as alias 'dir' and 'ls').
Get-Content
→ Gets the contents of a file.
Where-Object
→ Selects Objects from a collection based on their property values.
ForEach-Object
→ Performs an operation against each item in a collection of input objects.
Select-Object
→ Selects objects or object properties.
Out-File
→ Finds text in strings and files.
Out-Null
→ Sends output to a file.
Out-Grid
→ Sends output to an interactive table in a separate window.
New-Object
→ Creates an instance of a Microsoft .NET Framework or COM object.
Write-Host
→ Writes customized output to a host.
Write-output
→ Sends the specified objects to the next command in the pipeline. If the command is the last command in the pipeline, the objects are displayed in the console.
Pipelines
Cmdlets are most powerful when working together. The output from one command can be sent to another command through a "pipeline" by using the | character.
Examples
Variables
Variables are universal in-memory data storage, and variable names always start with a "$". Any command output can be stored in a variable for later use.
You can specify what type of variable to save by using the [_type]
notation. Powershell will automatically attempt to convert the output to that type of object is possible.
The following automatically attempts to parse an input file as an XML object.
You can save any object into variables, including .NET
and COM
objects, even applications.
Scripts
Scripts allow you to combine multiple commands into one easy to run file. Scripts can be entirely self contained or can take command-line arguments.
To start Powershell's script editor type powershell_ise.exe
in the command-line.
base64.ps1
base64_input.ps1
Functions
Functions allow you to create something in memory that looks, works, smells and feels almost exactly like a native Cmdlet. While it is not mandatory to do so, it is highly encourage to follow the Verb-Noun rules using approved verbs when naming functions.
The basic construct of a function is:
convert-StringToBase64.ps1
Modules
"A script module is a file (.psm1) that contains any valid Windows PowerShell code. Script developers and administrators can use this type of module to create modules whose members include functions, variables, and more"
Last updated