header image

Archive for February, 2018

PowerShell while

Posted by: | February 28, 2018 Comments Off on PowerShell while |

PowerShell has a number of looping structures – do; while; for; foreach. This is how the PowerShell while loop works The while statement has the form: while (<condition>){<statement list>}   The while loop is probably the simplest of the PowerShell loops. For example: $x = 0 while ($x -lt 5){ Write-Host “`$x is $x” $x++ […]

under: PowerShell

Get-Date format

Posted by: | February 27, 2018 Comments Off on Get-Date format |

A common question revolves around Get-Date format. In other words how can you format the output of Get-Date. Standard output is PS> Get-Date 27 February 2018 16:02:13   You can use –DisplayHint to control what’s displayed PS> Get-Date -DisplayHint Date 27 February 2018 PS> Get-Date -DisplayHint Time 16:03:09 PS> Get-Date -DisplayHint DateTime 27 February 2018 16:03:17 […]

under: PowerShell

Iron Scripter puzzles

Posted by: | February 27, 2018 Comments Off on Iron Scripter puzzles |

We keep innovating around the content of the PowerShell Summit to ensure it remains fresh and relevant to our attendees. This year we’re introducing the Iron Scripter competition. As a run up to the main competition I’ve written a number of challenges. The first half of the Iron Scripter puzzles are available. The puzzle is published […]

under: PowerShell

PowerShell Scope

Posted by: | February 26, 2018 Comments Off on PowerShell Scope |

PowerShell Scope has an important impact on the way your code runs. When you run a script or a function in PowerShell it runs it in its own scope. This means that all variables, functions, classes, aliases etc are removed from memory at the end of the script. Here’s an example create a class class […]

under: PowerShell

Controlled zip

Posted by: | February 23, 2018 Comments Off on Controlled zip |

Powershell v5 introduced the Compress- and Expand-Archive cmdlets which enabled you to manage compressed archives. I had a question about how you could control adding files to archives using a CSV file. This is how you do a controlled zip.   Start by creating a set of test data. 1..100 | foreach { $file = […]

under: PowerShell v5

PowerShell if

Posted by: | February 23, 2018 Comments Off on PowerShell if |

The PowerShell if statement enables you to branch your code depending in the results of one or more conditional tests. The tests can be anything you need but must produce a boolean – true/false – result. Also 0 is treated as $false and a positive non-zero is $true. A negative non-zero generates an error.   […]

under: PowerShell

ComputerName

Posted by: | February 22, 2018 Comments Off on ComputerName |

If you want to find the name of the local computer you use $env:COMPUTERNAME.   Except that doesn’t exist in Linux PowerShell v6 – you have to use $env:HOSTNAME   PowerShell 1 Consistency 0   I can live with having $env:HOSTNAME because I bet that’s what Linux users would look for. It would be nice […]

under: PowerShell v6

PowerShell for loop

Posted by: | February 15, 2018 Comments Off on PowerShell for loop |

PowerShell has a number of looping mechanisms – do-while; do-until; foreach; while and for loops. In this post I’ll show you how to use the PowerShell for loop. A for loop iterates a predefined number 0f times. A basic for loop looks like this: for ($i=0; $i -lt 10; $i++) { $i }   In […]

under: PowerShell Basics

PowerShell v6 and PowerShell Direct

Posted by: | February 13, 2018 Comments Off on PowerShell v6 and PowerShell Direct |

Not seen this reported anywhere so thought I post.   PowerShell v6 went to GA in January 2018. PowerShell Direct is a feature of Windows 10/Windows Server 2016. By accident I found that PowerShell v6 and PowerShell Direct work together.   PowerShell v6 is based on .NET core which is basically a subset of the […]

under: Hyper-V, PowerShell v6

Putting on the style

Posted by: | February 4, 2018 Comments Off on Putting on the style |

PowerShell is all about getting things done but how you do things can be as important as what you do. I’ll explain what I mean so you be able to be putting on the style.   While PowerShell is used by a number of developers its predominantly an administrators tool. Most administrators aren’t taught to code […]

under: Philosophy, PowerShell

Older Posts »

Categories