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 […]
Archive for June, 2018
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> […]
PowerShell string concatenation
Posted by: richardsiddaway | 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 […]
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 […]
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
PowerShell versions
Posted by: richardsiddaway | 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 / […]
PowerShell v6.1 preview 3
Posted by: richardsiddaway | 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 […]
PowerShell commands
Posted by: richardsiddaway | 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 […]
Creating collections for output
Posted by: richardsiddaway | 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]$_ […]
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 […]
Categories
- .NET
- Active Directory
- Architecture
- Azure
- Bash
- BITS
- Books
- CDXML
- CIM
- Cloud
- COM
- Containers
- Deep Dive
- Desired State Configuration
- DevOps
- DHCP
- DNS
- DSC
- European Summit
- Events
- Exchange
- File System
- Firewall
- General
- General IT Matters
- Hyper-V
- IIS
- Infrastructure
- IT Community
- IT Security
- Learning PowerShell
- Linux
- Math
- Microsoft
- Modules
- Nano Server
- Networking
- Office 2010
- Office 2013
- Open Source
- Opinion
- Outlook
- Philosophy
- PowerShell
- PowerShell 7
- PowerShell and .NET
- PowerShell and Active Directory
- PowerShell and CIM
- PowerShell and Exchange 2007
- PowerShell and IIS
- PowerShell and SQL Server
- PowerShell and WMI
- PowerShell Basics
- PowerShell original
- PowerShell Summit
- PowerShell User Group
- PowerShell User Group 2
- PowerShell v2
- PowerShell V3
- PowerShell v4
- PowerShell v5
- PowerShell v6
- PowerShell.org
- PowerShellGet
- PowerShellV2
- PSAM
- Rant
- Registry
- Scripting
- Scripting Games
- Scripting Games 2104
- Security
- SQL Server
- Storage
- Strings
- Summit
- Technology
- Uncategorized
- Virtualization
- Windows 10
- Windows 2012 R2
- Windows 7
- Windows 8
- Windows 8 Server
- Windows 8.1
- Windows Server
- Windows server 1709
- Windows Server 2008
- Windows Server 2008 R2
- Windows Server 2012
- Windows Server 2012 R2
- Windows Server 2016
- Windows Server 2019
- WMFv5
- WPF
- WSUS