In PowerShell, to measure time, you can simply subtract datetime values from another: $Start = Get-Date $null = Read-Host -Prompt "Press ENTER...
ps1
Using Solid Alternatives for $MyInvocation
Lines like $MyInvocation.MyCommand.Definition can be useful to determine the folder in which the current script is stored, i.e. to access other...
Finding Open Firewall Ports
Here is a piece of PowerShell code that connects to the local firewall and dumps the open firewall ports: $firewall = New-object -ComObject...
Executing Code with a Timeout (Part 2)
In the previous tip we implemented a timeout using PowerShell background jobs so you could set a maximum time some code was allowed to run before it...
Executing Code with a Timeout (Part 1)
If you’d like to make sure some code won’t execute forever, you can use background jobs to implement a timeout. Here is a sample...
Code-Signing Mini-Series (Part 5: Auditing Signatures)
Once a PowerShell script carries a digital signature, you can easily find out who signed the script, and more importantly, whether the script is...
Code-Signing Mini-Series (Part 4: Code-Signing PowerShell Files)
Before you give away a PowerShell script to others, it is a good idea to digitally sign it. A signature acts like a “wrapper” for your...
Code-Signing Mini-Series (Part 3: Reading Certificates from Personal Store)
Certificates can be installed permanently by loading them into Windows certificate store. PowerShell can access this store via its cert: drive. The...
Code-Signing Mini-Series (Part 2: Reading Certificates from PFX Files)
In the previous tip we created new code-signing test certificates both as pfx file and located in your certificate store. Today, you’ll see...
Code-Signing Mini-Series (Part 1: Creating Certs)
To play with digital signatures, and discover how you can sign scripts and modules, you first need a code-signing certificate. If you can’t get one...
Converting SecureString to Clear Text
Secure string content cannot be easily viewed: $password = Read-Host -Prompt 'Your password' -AsSecureString PS C:\> $password...
Using Catalog Files
Catalog file support (.cat) is new in PowerShell 5.1. Cat files basically are file lists with hash values. You can use them to ensure that a given...
Making Error Records More Readable
Whenever PowerShell encounters an error, it emits an Error Record with detailed information about the problem. Unfortunately, these objects are a...
Creating Write-Protected Functions
PowerShell functions by default can be overridden anytime, and you can also remove them using Remove-Item: function Test-Lifespan {...
$FormatEnumerationLimit Scoping Issues
As shown in the previous tip, the secret $FormatEnumerationLimit variable determines how many array elements are shown in output before the output...
Displaying Array Members in Output
When you output objects that have arrays in their properties, only 4 array elements are displayed, then an ellipsis truncates the rest: PS C:\>...
Find All Files Two Levels Deep
Yet another file system task: list all *.log files in a folder structure, but only to a maximum depth of 2 folder structures: Get-ChildItem -Path...
Deleting All Subfolders Below A Given Level
Here is another file system task that sounds worse than it actually is. Let’s say you need to remove all folders below a given level in a...
Deleting All Files from a Folder Structure
Sometimes tasks sound worse than they actually are. Let’s say you need to clear a folder structure and remove all files, leaving empty...
Adding RSS Ticker to PowerShell Title Bar
A new PowerShell background thread can do things for you in the background, for example updating your PowerShell window title bar with new news...
Invoke-WebRequest vs. Invoke-RestMethod
Invoke-WebRequest simply downloads the content from any web site. It is then your job to read the content and make sense of it. In the previous tip...
Downloading PowerShell Code
In the previous tip we explained how Invoke-WebRequest can be used to download the raw HTML content for any web page. This can also be used to...
Hiding Progress Bars
Sometimes, cmdlets automatically display a progress bar. Here is an example of such a progress bar: $url =...
Analyzing Web Page Content
PowerShell comes with a built-in web client which can retrieve HTML content for you. For a simple web page analysis, use the -UseBasicParsing...