Monthly Archives: June 2018

PowerShell if not

When you’re using an if statement you’re usually testing for a positive so how do you do a PowerShell if not There a few scenarios to cover. The simplest is if you’re testing a boolean: PS> $x = $true if … Continue reading

Posted in Powershell | Leave a comment

PowerShell sleep

Getting PowerShell to sleep or pause during execution can sometimes be useful. This is how you can do a PowerShell sleep. The starting point is Start-Sleep PS> Get-Command Start-Sleep -Syntax Start-Sleep [-Seconds] <int> [<CommonParameters>] Start-Sleep -Milliseconds <int> [<CommonParameters>] You can … Continue reading

Posted in Powershell | Leave a comment

PowerShell string concatenation

PowerShell string concatenation is performed using the concatenation operator which is + PS> $a = ‘1234’ PS> $b = ‘5678’ PS> $c = $a + $b PS> $c 12345678 When you concatenate 2 strings you’re creating a new string not … Continue reading

Posted in Powershell | Leave a comment

Write-Host

Write-Host has had a bad press over the years culminating in the infamous saying “if you use Write-Host a puppy will die” or words to that effect. So what’s the fuss about? Let’s take some code Write-Host -Object “starting” function … Continue reading

Posted in Powershell | 1 Comment

Hyper-V switches

I need to do some work on the Hyper-V switches in my lab so need to see which VMs are on which switch. Easier than I thought: Get-VM | Get-VMNetworkAdapter | select VMname, Name, Switchname

Posted in Hyper-V, Powershell | Leave a comment

PowerShell versions

PowerShell has gone through a number of versions since it was first released in November 2006. These are the major features of the PowerShell versions: PowerShell v1 – initial release with 137 cmdlets. (released with Windows Vista / Server 2008 … Continue reading

Posted in Powershell | Leave a comment

PowerShell v6.1 preview 3

PowerShell v6.1 preview 3 became available a couple of weeks ago. https://github.com/PowerShell/PowerShell/releases To the user its a minor set of fixes from preview 2 though if you read the release notes there’s a lot of work going on in the … Continue reading

Posted in PowerShell v6 | Leave a comment

PowerShell commands

When you think of PowerShell commands most people think of cmdlets but that’s not the full story. PowerShell commands encompass: Aliases Functions Cmdlets Native Windows commands When you type a command name the command types listed above are checked in … Continue reading

Posted in Powershell | Leave a comment

Creating collections for output

There are many times when you end up creating a collection – either for output or use for performing further processing. There’s a common pattern that’s used when creating collections for output that’s very inefficient. That pattern is: $data = … Continue reading

Posted in Powershell | 2 Comments

Creating objects

In my recent post about avoiding Add-Member I showed how to create objects. As with most things in PowerShell there are a number of different ways of creating objects. I showed this code: $os = Get-CimInstance -ClassName Win32_OperatingSystem$comp = Get-CimInstance … Continue reading

Posted in Powershell | Leave a comment