PowerShell – How to Create an Array with PSObject

As I told you before in my previous blog post, I was asked to build an interactive PowerShell script for creating Virtual Machines in Azure. In this blog post, I want to show you how I’ve created a report (or array) within PowerShell that:

  • Visualize the to-be-created objects to the user
  • Allows PowerShell to get the data of that array to create Virtual Machines. This makes sure that you have a consistent view of what PowerShell will create for you. See it as an order overview before you buy something online.

Let’s imagine I have all the Virtual Machines that I want to create in $VMs. This can be an import of a CSV, or maybe I’ve asked the user for details with “Read-Host” or Out-GridView. When you import a CSV, the content will already be organized in an array. But with this code, you can easily add more content to it and combine both data from a CSV and from the script. For example, a randomized password or the first available IP address in a subnet in Azure. My end goal is to have a nice overview of all the needed Virtual Machines in $Report, which I can later use to make those VMs. With the code below, you will create a PSObject for every VM in the $VMs variable. After the PSObject has been created, it will append to the $Report variable. With “$Report = @()” you ask PowerShell to create an empty array. See it as an empty table that you could later use to add content to it. After the deployment has succeeded or failed, you can add the status to $VM.DeploymentStatus.

# Set report variable
$Report = @()

Foreach ($VM in $VMs) {
    $PSObject = New-Object PSObject -Property @{
        DeploymentName          = $VM.resourceGroupName + "-" + (Get-Date -Format "yyyyMMdd-hh-mm-ss")
        VMName                  = $VM.vmName
        Location                = $VM.resourceGroupLocation
        ResourceGroupName       = $VM.resourceGroupName
        AdminPassword           = $VM.adminPassword
        VMSize                  = $VM.vmSize
        VirtualNetwork          = $VM.virtualNetwork
        VirtualNetworkRG        = $VM.virtualNetwork.ResourceGroupName
        SubnetName              = $VM.subnetName
        IPAddress               = $VM.ipAddress
        OperatingSystem         = $VM.operatingSystem
        DeploymentStatus        = $Null
    $Report += $PSObject

# Show the report

# Or show it in Table Format
# $Report | Format-Table

The above example is by far the easiest way to create a nice array for me.

Thanks for reading. Hope you find it useful too.

PowerShell – Using Out-GridView to Select a Parameter

Last week I was asked to build an interactive PowerShell script for creating Virtual Machines in Azure. In this blog post, I want to share an easy way to prompt a user for a selection.

# Select Azure subscription
$AzureSubscription = (Get-AzureRmSubscription | Out-GridView -Title "Choose your Azure subscription and click OK." -PassThru)
Write-Output "Switching to Azure subscription: $($AzureSubscription.Name)"
$AzureSubscriptionInfo = Select-AzureRmSubscription -SubscriptionId $AzureSubscription.Id

This uses Out-GridView to display the contents of the “Get-AzureRmSubscription” Cmdlet and asks the user to make a selection. The user is able to sort and filter the contents within the grid and the user will be informed of the decision by using “Write-Output”.


Let’s say it’s not the most elegant way to ask a user to select a value because it’s a pop-up and because of the small “OK” and “Cancel” buttons, but this PowerShell script was developed for IT Administrators. The benefit is that it’s easy to use with out-of-the-box code, instead of using custom modules.

That’s it for now, hope you find it useful.


PowerShell Function to Restart a Process

My notebook connects to a Docking Station with access to my receiver with speakerset, 2 screens, power and a KVM switch for my mouse and keyboard. When I lock my laptop, the sounds switches from the receiver to my internal speakers. When I unlock my laptop, the sound switches back but the Spotify application doesn’t play any sound. Closing the application doesn’t solve this problem, because the application will crash and I have to use the Task Manager to force the application to close. I made a PowerShell function that I’ve added to my PowerShell profile.

The Restart-Spotify function looks for any process that ends with “spotify” and stops the process. When all the processes are killed, a new instance of Spotify will be opened and the PowerShell console will close itself.

Even a reinstall of Spotify doesn’t help solving this issue I’m facing for months now. So the above script is a great workaround for me.

How to Clear a TPM 2.0 chip with SCCM and PowerShell

With TPM 1.2, Microsoft was able to clear the TPM during the SCCM Task Sequence without asking for permission to clear the TPM. With TPM 2.0, SCCM is unable to clear and activate the TPM chip during the deployment. The first time you boot your computer, you need to provide a BitLocker Recovery Key, or the tpm.msc console will tell you that the TPM is ready for use, with reduced functionality.

Continue reading “How to Clear a TPM 2.0 chip with SCCM and PowerShell”

PowerShell Profiles – The structure of your _PSH_BASE.ps1 file

In my previous post PowerShell Profiles – The profile.ps1 file I showed you my profile.ps1 file. In this post, I’ll show you a way to structure your base file, so that you can use it for your functions and aliases. Make sure that you always use max 2 files. 1The first file is your profile.ps1 file and the other file is this _PSH_BASE.ps1 file. If you use like 3 or 4 files, it can take a couple of seconds to load your PowerShell session.

Continue reading “PowerShell Profiles – The structure of your _PSH_BASE.ps1 file”

PowerShell Profiles – The profile.ps1 file

Welcome to this blog series about PowerShell profiles. I’m using PowerShell profiles for a couple of months now to make life a lot easier. To start this blog series, I would like to show you my Profile.ps1 file. It’s located in “C:\Users\<Your username>\Documents\WindowsPowerShell”. Because I use my PowerShell profiles at multiple locations such as my work notebook, home computer and sometimes at projects, I need to make sure that my PowerShell script home is always right so that the rest of the PowerShell profile is able to load successfully. That’s why I have these commands at the beginning of my profile:

Continue reading “PowerShell Profiles – The profile.ps1 file”

SCCM 2012 R2 SP1 – Make Windows 8.1 drivers supported on Windows 10 with PowerShell

I had an issue within my lab with deploying Windows 8.1 drivers to Windows 10 with SCCM 2012 R2 SP1. It isn’t possible to make all Windows 8.1 drivers compatible with Windows 10 within the SCCM 2012 R2 SP1 console with just one click. Because I was running within a lab environment and I only had 2 driver packages for Windows 8.1 x64, I was able to make the drivers available for deployment to all platforms. You can do this with the magic of PowerShell:

Continue reading “SCCM 2012 R2 SP1 – Make Windows 8.1 drivers supported on Windows 10 with PowerShell”