PowerShell for Windows Admins


August 29, 2019  11:25 AM

Testing Windows activation

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
CIM, Powershell

Testing Windows activation from PowerShell involves a little dive into CIM (WMI).

At its simplest a function like this

function test-activation {

$ta = Get-CimInstance -ClassName SoftwareLicensingProduct -Filter “PartialProductKey IS NOT NULL” |
Where-Object -Property Name -Like “Windows*”

if ($ta.LicenseStatus -eq 1) {$true} else {$false}
}

Checks the SoftwareLicensingProduct class for instances with a Partial product key. Where-Object filters on the Name of the product. It has to start with Windows.

If the LicenseStatus equals 1 then Windows is activated – otherwise it isn’t

August 29, 2019  9:21 AM

Get-AdUser in PowerShell Core

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Active Directory, Powershell

There has been a problem with Get-ADUser in PowerShell core such that

Get-ADUser -Identity Richard -Properties *

Throws an error.

The problem is in .NET Core and affects a small number of properties including ProtectedFromAccidentalDeletion

The underlying .NET Core issue has been fixed and PowerShell v7 preview 3 on Windows 10.0.18362 will successfully run the command


August 28, 2019  12:06 PM

Windows Terminal v0.4.2382.0

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Windows Terminal v0.4.2382.0 has ben released to the Microsoft store. if you have Windows Terminal installed it should automatically update for you.

Copying out of a window using the keyboard shortcuts now works – thank you – that makes the whole thing much more usable.

I can’t seem to find a way to make the font have a bold face which is really good for demonstrations.

Windows terminal automatically picks up my Windows v6.2 and Windows PowerShell v5.1 instances as well as WSL and the command line. PowerShell v7 previews are ignored but could be configured manually. Given the rate of change in the preview versions not sure that its a good idea to add them to the terminal setup.

So far the experience is reasonably positive for a product still under development.

I’d recommend you give it a try if you haven’t already done so.


August 23, 2019  1:12 PM

Experimental features

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

PowerShell core has recently. v6.2, had the concept of experimental features added. An experimental feature is new or changed functionality that may be a breaking change or about which the PowerShell team want feedback before finalising the code.

My PowerShell v6.2.2 instance has the following experimental features

PSImplicitRemotingBatching
PSUseAbbreviationExpansion
PSCommandNotFoundSuggestion
PSTempDrive

of which I’ve enabled PSCommandNotFoundSuggestion and PSTempDrive. Experimental features are disbaled by default in PowerShell v6.2

My PowerShell v7 preview 3 instance has the following experimental features

PSImplicitRemotingBatching
PSForEachObjectParallel
PSCommandNotFoundSuggestion

of which PSForEachObjectParallel and PSCommandNotFoundSuggestion

PSUseAbbreviationExpansion and PSTempDrive and now full features in PowerShell v7

If don’t have any PowerShell v6.2 experimental features enabled OR don’t have v6.2 installed and then install PowerShell v7 preview 3, or later, all experimental features will be enabled by default. If you’ve enabled any experimental features in PowerShell v6.2 then PowerShell v7 respects the settings and doesn’t enable all experimental features.

You can view the settings controlling experimental features (among other things) at

Get-Content –Path $home\Documents\PowerShell\powershell.config.json

The single settings file is used for PowerShell v6.2 and PowerShell v7 previews


August 21, 2019  12:30 AM

PowerShell v7 preview 3

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

PowerShell v7 preview 3 is now available from https://github.com/PowerShell/PowerShell/releases

Breaking changes seem to be confined to non-Windows platforms with the removal of the kill alias on Stop-Process and support for pwsh as a login shell

The big new item is the –parallel parameter on Foreach-Object – more on this later.

In this preview version and future preview versions all experimental features will be enabled going forward


August 15, 2019  1:22 PM

Out-GridView is back

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Out-GridView is finally back in PowerShell core – https://devblogs.microsoft.com/powershell/out-gridview-returns/.

The project is hosted on github – https://github.com/powershell/GraphicalTools

Install the module from the gallery –

PS> Install-Module -Name Microsoft.PowerShell.GraphicalTools

Currently, Out-GridView is the only command in the module though adding Show-Command and Show-Object are planned.

The module works cross-platform not just Windows.

You need PowerShell v6.2 or later to build or presumably run the module.


August 13, 2019  9:22 AM

PowerShell v2

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
PowerShell 2

Just seen a question about PowerShell v2. PowerShell v2 was a huge step forward when it appeared in October 2009 as part of Windows 7 / Server 2008 R2

Windows 7 support finishes 14 January 2020

Windows Server 2008 R2 support finishes 14 January 2020

That’s less than 6 months.

PowerShell v2 isn’t mentioned in the Microsoft documentation which starts at PowerShell v3

If you’re still using PowerShell v2 its beyond time to move to a later version.


August 9, 2019  4:24 AM

Test if a transcript is running

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

PowerShell has the ability to create a transcript of the commands you run at the console and the results displayed in the console. But how can you test if a transcript is running?

It used to be that you could only have a single transcript running but Windows PowerShell v5.1 and PowerShell v6.x and later allow multiple transcripts to be running in the same session.

Only way I can think of testing if transcript has been started is to use

Get-History | where CommandLine -like ‘Start-Transcript*’

that doesn’t tell if its still running for which you need

Get-History | where CommandLine -like ‘Stop-Transcript*’

PS> (Get-History | where CommandLine -like ‘Start-Transcript*’).Count – (Get-History | where CommandLine -like ‘Stop-Transcript*’).Count

should give a result of zero if no transcripts are running. A positive result indicates transcripts are running. A negative result indicates problems.

The test can be wrapped in a function

function test-runningtranscript {
$starts = (Get-History | Where-Object CommandLine -like ‘Start-Transcript*’).Count
$stops = (Get-History | Where-Object CommandLine -like ‘Stop-Transcript*’).Count

$trans = $starts – $stops

switch ($trans){
0 {$false}
{$_ -gt 0} {$true}
{$_ -lt 0} {Throw “Error!!! Can’t have negative transcripts”}
}
}

You’ll get True returned if there is a transcript running and False if there isn’t.


August 5, 2019  10:38 AM

Identifying the host

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Identifying the host in which your PowerShell code is running could be important. For instance you might not want to run some code that takes a long time to complete in VSCode – you may prefer to ensure it runs in the console as it consumes fewer resources.

You can identify the host – most of the time – using $host

For the PowerShell console – Windows PowerShell or PowerShell Core

PS> $host.Name
ConsoleHost

For ISE – Windows PowerShell

PS> $host.Name
Windows PowerShell ISE Host

For VScode – Windows PowerShell or PowerShell Core

PS> $host.Name
Visual Studio Code Host

For the new Windows Terminal – Windows PowerShell or PowerShell Core

PS> $host.Name
ConsoleHost

For you need to differentiate between the traditional PowerShell console and the new Windows terminal you’ll find that the Windows terminal adds an environmental variable

WT_SESSION

which takes a value of the form – 407c1756-556e-4df2-97db-c159a616b237


August 3, 2019  9:20 AM

PowerShell Day UK 2019

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The PowerShell Day UK 2019 one day conference is on Saturday 28 September 2019 – https://psday.uk/

I’ll be speaking and willing to answer any PowerShell questions that I can during the breaks.

If you have any books of mine that you want signing – bring them along and I’ll be happy to oblige.


Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: