PowerShell Snippets Vol 3

This is my PowerShell Snippets Vol 3. A collection of PowerShell commands for tasks that I don’t perform often and can’t recall easily from memory. Those ‘I know I’ve worked this out before’ type moments. Volume 1 is available here and Volume 2 is available here.

A quick recap.

I live in PowerShell and my memory is pretty good. There are a number of common PowerShell commands and one-liners or functions that I use a lot and I can remember them. However, then there are the ones I use less regularly and I often find myself trying to recall the last time I used them in a script, in order to locate that script just to get those couple of lines. So I’m posting a bunch more of them in this post, if for nothing else to help me find them quickly.

Installing / Upgrading PowerShell using WinGet

There are a number of PowerShell packages. In order to install using WinGet  you must specify the exact ID (and optionally the version). Here is an example to install v7.0.3 of PowerShell using WinGet.

winget install -e --id Microsoft.PowerShell -v 7.0.3

Raw x509 Certificate Data Conversion

Previously in Windows PowerShell you could take a raw x509 Certificate (in PEM/CER format) and use the System.Security.Cryptography.X509Certificates.X509Certificate2 method to inspect the details. Where $cert is the certificate data as a Base64 string eg.

"MIIDRTCCAi2gAwIBAgIQXEMsgYlmnZRAIWvVI86jTDANBgkqhkiG9w0BAQsFADAjMSEwHwYDVQQDDBhwd3NoLmRhcnJlbmpyb2JpbnNvbi5jb20wHhcNMjAwNzA5MDIzNjM5WhcNMzAwNzA5MDI0NjM4WjAjMSEwHwYDVQQDDBhwd3NoLmRhcnJlbmpyb2JpbnNvbi5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2FVRZVPXJFYPIeR6E520 < truncated > 28gUyRJ1AH5ysEBkrjHXLfhrnI4ruQ5xeu0OsZdXeP45rwX9eD5eQf42PJ0IN4wt+eRwXETbQo+vaUHyRkXjk3CJRF43IhIThHVEw0lqKWS5GNz+ZiKHecBDlWFcCyfVG7R3VsWuD0t+9AMgkPZxkTXpaPmc7tKNbpp91dcyxtGpsx6LFqbrwapPQppgg5VUaMqwaN02o74HY7NabjyP+LAjQ8buMNK5YfvpK+OULg2u5QV9cvKM/DvUm6VwQ9l9o6genWUd+9A2sNCPuRkmKhYe9gjZMYcOTdXVQ=="

the following would then convert the Base64 string to a readable Certificate object.

$certData = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 

In PowerShell Core / PowerShell 7+ the following will achieve the same outcome. Note it is now a one-liner and the the .import method is no longer used.

$certData = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2(, [Convert]::FromBase64String($cert))

If you want a handy little PowerShell Module that will handle this for you (on Windows PowerShell and PowerShell Core / PowerShell 7+) then checkout my x509Details PowerShell Module.

PowerShell / PowerShell Core WebRequests to URI’s with Self Signed Certs

In Windows PowerShell in order to connect to URI’s that utilise Self-Signed Certificates using Invoke-WebRequest or Invoke-RestMethod the following workaround was used;

add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
   public class TrustAllCertsPolicy : ICertificatePolicy {
   public bool CheckValidationResult(
      ServicePoint srvPoint, X509Certificate certificate,
      WebRequest request, int certificateProblem) {
      return true;
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

In PowerShell Core / PowerShell 7 ICertificatePolicy does not exist in .NET Core. When attempting to use the Windows PowerShell workaround above you get the error;

Add-Type: (3,56): error CS0426: The type name ‘CertificatePolicy’ does not exist in the type ‘ServicePointManager’
public class TrustAllCertsPolicy : ServicePointManager.CertificatePolicy {

Add-Type: Cannot add type. Compilation errors occurred.

The type name 'CertificatePolicy' does not exist in the type 'ServicePointManager'

To achieve the same both Invoke-WebRequest and Invoke-RestMethod now include the -SkipCertificateCheck switch.

So simply use;

Invoke-RestMethod [URL] -SkipCertificateCheck
Invoke-WebRequest [URL] -SkipCertificateCheck

Enforcing TLS on PowerShell Core /  PowerShell

A PowerShell Core / PowerShell (i.e. NOT Windows PowerShell) version of allowing TLS, TLS 1.1, TLS 1.2 and TLS 1.3.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13

Detecting Remote Network Connections to a URL

Look to see if there are open network connections to a public URL.

$server = 'login.microsoftonline.com'
$serverAddresses = Resolve-DNSName $server | select-object IP4Address -Unique | Where-Object {$_.IP4Address -ne $null}
$remoteConnections = Get-NetTCPConnection | select-object | where-object {$serverAddresses.IP4Address.Contains($_.RemoteAddress)} 

That’s it for PowerShell Snippets Vol 3. I’ll start compiling others as I search for them and don’t find them in this Vol or Vol 1 or 2.