header image

Archive for June, 2018

PowerShell if not

Posted by: | June 30, 2018 Comments Off on 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 ($x) {‘Yes’} else {‘No’} Yes   In an if statement the test ($x) is evaluated […]

under: PowerShell

PowerShell sleep

Posted by: | June 30, 2018 Comments Off on 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 set a time in seconds or milliseconds that you want PowerShell to pause PS> […]

under: PowerShell

PowerShell string concatenation

Posted by: | June 30, 2018 Comments Off on 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 adding extra characters to the end of the first string. My preference is to […]

under: PowerShell


Posted by: | June 29, 2018 Comments Off on 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 t1 { Write-Host -Object “In the function” 2+2 } Write-Host -Object “pre-function” t1 Write-Host -Object […]

under: PowerShell

Hyper-V switches

Posted by: | June 29, 2018 Comments Off on 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

under: Hyper-V, PowerShell

PowerShell versions

Posted by: | June 29, 2018 Comments Off on 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 – not Server Core). Only way to work remotely was Get-WmiObject. no longer available. PowerShell v2 (Windows 7 / […]

under: PowerShell

PowerShell v6.1 preview 3

Posted by: | June 28, 2018 Comments Off on 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 background. If the 6 months between releases idea holds we can expect to see the […]

under: PowerShell v6

PowerShell commands

Posted by: | June 25, 2018 Comments Off on 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 order to determine the command to run – specifically aliases, then functions, then cmdlets and […]

under: PowerShell

Creating collections for output

Posted by: | June 24, 2018 Comments Off on 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 = @() 97..122 | foreach { $props = @{ Number = $_ Letter = [char][byte]$_ […]

under: PowerShell

Creating objects

Posted by: | June 21, 2018 Comments Off on 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 -ClassName Win32_ComputerSystem $props = @{ OS = $os.Caption Version = $os.Version Name = $comp.Caption […]

under: PowerShell

Older Posts »