Blog ❯ Author: Fabio Moschini|Date: 08.12.2022
PowerShell examples
Powershell

PowerShell script examples
Version
To display the PowerShell version installed on the computer, you can use:Online help
The command:provides information about commands (cmdlets) and their syntax.Example:
Create a directory
Use the cmdlet:If you only specify the directory name, it will be created in the current directory.The -ItemType option defines the type of object to create.
To create a file, you can use:
Copy a directory
To copy a directory with all its content, use the cmdlet:The copy can be between drives or within the same drive.Remove a directory
To delete a directory with all its content, use:If you omit -Recurse and the directory contains items, confirmation will be requested.Move a directory
Use:Rename a directory
Use the cmdlet:Check if a directory exists
Use:Output will be true if the directory exists, otherwise false.Create a file
Use New–Item as shown above to create files:Copy a file between directories
Use:If you omit the destination filename, the file keeps its original name.Delete a file
Use:Move a file
Use the same cmdlet as for directories:If you omit the destination filename, the file keeps its original name.Rename a file
Use:Check if a file exists
Use:Output will be true if the file exists, orfalse if not.Read file contents
Use:Show current date and time
Use:By default the full format for date and time is displayed.To display date and time in short format:To display only the current date, use -DisplayHint Date:To display only the current time, use:
Change system date/time
You can set the system date/time with:To move system time back by 15 minutes:Warning
To use Set-Date you must run PowerShell as an administrator.Create an XML file
We've already seen how to create a file with New–Item. To create an XML file specify the extension:To add content to the new file useSet-Content:To read the file content use:Clear file contents
Use:Append text to a file
Use:Variables
Variables in PowerShell start with $ followed by letters, numbers or _. To use special characters in a variable name, wrap the name in { and}. Example:Assign values with =:You can assign multiple variables at once:Also assign multiple values to a variable to create an array:Access an element by index:The value will be 134. Sort and remove duplicates with:Get the count with:Initialize a consecutive integer array with:To slice an array:Measurements
PowerShell provides Measure-Object for different measurements. Examples:- Count files and folders in the current directory:
- Get minimum/maximum file size, sum and average:
- Count characters, words and lines in a text file:
List directory contents
Use Get-ChildItem:Omitting the path lists the current directory: Filter by extension:Compare two text files
Use:Pause script execution
Use for milliseconds or for seconds.Aliases
Aliases are shortcuts for cmdlet names. For example create an alias for Get-Help:Aliases are valid only for the current session. Use Export-Aliasto save them and to list them.PowerShell backtick
PowerShell supports escape sequences starting with the backtick `. Common sequences include:| Sequence | Description |
|---|---|
| `t | Horizontal tab |
| `n | New line |
| `v | Vertical tab |
| `b | Backspace |
| `e | Escape |
| `0 | Null |
| `a | Alert |
| `f | Form feed |
| `r | Carriage return |
| `u{x} | Unicode escape sequence |
Example:Output:
This text appears on the first line
Second line Tab example
Second line Tab example
String operations: Split and Join
Split divides a string using a character or substring. In PowerShell use the -split operator.For example split a sentence into words:Output:
This
string
processed
with
the
-split
string
processed
with
the
-split
You can split using another substring:The output is:
Thi
s
ring after being
split
s
ring after being
split
The opposite operation join merges strings. Example:Output:
String obtained using -join