PowerShell for Windows Admins


May 27, 2019  1:48 PM

Get-Date -UFormat

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The Get-Date -UFormat parameter formats the date using Unix format. The full list of format specifiers are in the Notes section of the Get-Date help file.

Some examples are:

PS> Get-Date -UFormat ‘%Y-%m-%d %H:%M%:%S%Z’
2019-05-27 20:40:11+01

4 digit year – two digit month and day. Hour in 24 hour format and minutes and seconds and finally time zone as offset from GMT

Date in locale format

PS> Get-Date -UFormat %x
05/27/19

though it shows a US format for a UK locale?

PS> Get-Date -UFormat ‘%A %d %B %Y’
Monday 27 May 2019

day of week – day – Month (full name) 4 digit year.

Between the Format parameter and the UFormat parameter you should be able to get the date information into any format you require

May 27, 2019  5:58 AM

Get-Date –Format

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Get-Date –Format enables you to control the formatting of the datetime object returned by the cmdlet.

A standard call to get date returns:

PS> Get-Date

27 May 2019 12:36:47

The –Format parameter takes a value from the DateTimeFormatInfo class – https://docs.microsoft.com/en-us/dotnet/api/system.globalization.datetimeformatinfo?view=netframework-4.8 – to specify how you want the information to be formatted.

For instance:

Short and long forms of the date with no time

PS> Get-Date -Format d
27/05/2019
PS> Get-Date -Format D
27 May 2019

Full date and short or long time

PS> Get-Date -Format f
27 May 2019 12:48
PS> Get-Date -Format F
27 May 2019 12:48:38

No year or time

PS> Get-Date -Format m
27 May
PS> Get-Date -Format M
27 May

RFC1123 compliant

PS> Get-Date -Format R
Mon, 27 May 2019 12:50:07 GMT
PS> Get-Date -Format r
Mon, 27 May 2019 12:50:10 GMT

Sortable

PS> Get-Date -Format s
2019-05-27T12:50:45

Short and long time only

PS> Get-Date -Format t
12:51
PS> Get-Date -Format T
12:51:14

Sortable Universal time

PS> Get-Date -Format u
2019-05-27 12:51:55Z

Z refers to the time zone

Specify day month year format

PS> Get-Date -Format ‘dd/MM/yyyy’
27/05/2019


May 26, 2019  3:37 PM

Exclusive OR

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

PowerShell has a number of logical operators – – -and, –or, –not (or !). One I’ve really thought about is the exclusive OR operator –xor.

With a standard –or operator the result is TRUE if one of the statements is TRUE

PS> (‘a’ -eq ‘A’) -or (‘a’ -eq ‘z’)
True

The standard –or operator is also TRUE if both statements are true

PS> (‘a’ -eq ‘A’) -or (‘Z’ -eq ‘z’)
True

The exclusive OR is only TRUE when one of the statements is TRUE and the other is FALSE. So this returns TRUE

PS> (‘a’ -eq ‘A’) -xor (‘a’ -eq ‘z’)
True

But when both statements are TRUE it returns FALSE

PS> (‘a’ -eq ‘A’) -xor (‘Z’ -eq ‘z’)
False

The exclusive OR seems like bit of an oddity to me.

PowerShell has a bitwise xor operator — -bxor but outside of that I’m struggling to find a use for –xor. Certainly as an admin I can’t think of a situation where I’d use it.

Just out curiosity have you used –xor and if so how?


May 26, 2019  2:01 PM

Useful constants

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

PowerShell provides easy access to some useful constants. I often see people calculating these values rather than using the constants.

PowerShell recognises kb, mb, gb, tb and pb for kilobyte, megabyte, gigabyte, terabyte and petabyte respectively. You can use them like this:

PS> 1kb; 1mb; 1gb, 1tb, 1pb
1024
1048576
1073741824
1099511627776
1125899906842624

Don’t leave a space between the value and the constant.

You can use them in calculations:

PS> 7247437567256292 / 1gb
6749702.21450207
PS> 7247437567256292 / 1tb
6591.50606884968
PS> 7247437567256292 / 1pb
6.43701764536101

Fractional values are allowed:

PS> 27.457gb
29481729261.568

You can use upper case or lower case to denote the constant

PS> 27.457gb
29481729261.568
PS> 27.457GB
29481729261.568

Next time you need to work with megabytes or other common constants don’t calculate them use the constants in PowerShell


May 25, 2019  3:54 PM

Articles published in 2019

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

I’ve had the following articles published in 2019

https://searchwindowsserver.techtarget.com/tutorial/Set-up-users-with-key-PowerShell-Active-Directory-commands

https://searchwindowsserver.techtarget.com/feature/Editing-content-with-Windows-PowerShell

https://searchwindowsserver.techtarget.com/tip/Windows-Compatibility-module-expands-PowerShell-Core-reach

Enjoy


May 25, 2019  3:42 PM

Stable sort

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

In Windows PowerShell if you do something like this:

PS> (1..20 | Sort-Object -Property {$_ % 3}) -join ‘ ‘
9 6 12 15 3 18 19 16 13 10 4 1 7 20 17 2 8 11 5 14

The results come back in an unexpected order. This is not a stable sort as the results are sorted by their modulus result but lose the order within each group.

If you want a stable sort – where the results come back from a calculation like this in the order in they were received you need to add the –Stable switch. BUT that was added to PowerShell v6.1 and later

PS> (1..20 | Sort-Object -Property {$_ % 3} -Stable ) -join ‘ ‘
3 6 9 12 15 18 1 4 7 10 13 16 19 2 5 8 11 14 17 20

The results are sorted by modulus result and correctly within each group of modulus results

You could also use the Top or Bottom parameters:

PS> (1..20 | Sort-Object -Property {$_ % 3} -Top 20 ) -join ‘ ‘
3 6 9 12 15 18 1 4 7 10 13 16 19 2 5 8 11 14 17 20
PS> (1..20 | Sort-Object -Property {$_ % 3} -Bottom 20 ) -join ‘ ‘
3 6 9 12 15 18 1 4 7 10 13 16 19 2 5 8 11 14 17 20

If you alter the values presented to Top and Bottom you’ll get a subset of the results

PS> (1..20 | Sort-Object -Property {$_ % 3} -Top 10 ) -join ‘ ‘
3 6 9 12 15 18 1 4 7 10
PS> (1..20 | Sort-Object -Property {$_ % 3} -Bottom 10 ) -join ‘ ‘
13 16 19 2 5 8 11 14 17 20

Sort-Object in PowerShell Core has an interesting set of additions compared to Windows PowerShell.


May 25, 2019  12:00 PM

OpenSSH installation

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
OpenSSH, Powershell

OpenSSH installation has got a lot simpler in Windows 10 1809; Windows Server 2019 and Windows Server 1809.

OpenSSH is available as an optional feature. The client is preinstalled when you install the operating system. You just need to install the server:

Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

Installing the optional feature creates the required firewall rule which is good.

You still need to make changes to the sshd_config file to enable password and pubkey authentication. The subsystem isn’t configured for PowerShell.

There’s a bug in OpenSSH so that subsystem paths with spaces aren’t parsed so you need to create a symbolic link for the PowerShell v6 folder.

if you want to use key-pair authentication the OpenSSHUtils module on the PowerShell gallery has bugs so you need to manually set the permissions on the authorized_keys file.

SSH remoting has a lot to offer but incorrect documentation, the work needed to install and configure it and buggy software will stop people using it. On the plus side OpenSSH is getting easier to install and configure but its still a long way from good.


May 23, 2019  1:20 PM

Create a symbolic link

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

I recently had to create a symbolic link to overcome a bug in OpenSSH whereby OPENSSH won’t work with the path C:\Program Files\PowerShell\6\pwsh.exe because it has a space.

The answer is to create a symbolic link which is a file that contains a reference to another file (or directory):

New-Item -ItemType SymbolicLink -Path C:\pwsh -Target ‘C:\Program Files\PowerShell\6’

C:\pwsh is the symbolic link and the Target parameter has the original path. You can now use C:\pwsh in place of C:\Program Files\PowerShell\6

New-Item can also create a Hardlink or a Junction.

HardLink is a directory entry that associates a name with a file on disk

Junction is an NTFS artefact that is a pointer to a directory on the local volume


May 21, 2019  12:26 PM

PowerShell Core v6.2.1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

PowerShell Core v6.2.1 has been released – https://github.com/PowerShell/PowerShell/releases

as has v6.1.4

The new versions are to primarily fix the Security Vulnerability CVE-2019-0733 – https://portal.msrc.microsoft.com/en-US/security-guidance/advisory/CVE-2019-0733

v6.2.1 also enables tab completion for functions


May 19, 2019  12:56 PM

Windows Server 2019 updates with CIM

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
CIM, Powershell

Windows Server 2019 updates with CIM remain the same as all server versions post Windows Server 2016. This code will check for and install any updates. Micorosft Update or WSUS will be used depending on how your system is configured

$au = Invoke-CimMethod -Namespace root/microsoft/windows/windowsupdate -ClassName MSFT_WUOperations -MethodName ScanForUpdates -Arguments @{SearchCriteria=”IsInstalled=0″}
$au.Updates

if ($au.Updates.Length -gt 0) {
Invoke-CimMethod -Namespace root/microsoft/windows/windowsupdate -ClassName MSFT_WUOperations -MethodName InstallUpdates -Arguments @{Updates = $au.Updates}
}
else {
Write-Warning “No updates available”
}

This code should work on Server 1709, 1803, 1809 and Windows Server 2019.

It won’t work on Windows Server 2016 as the CIM classes were changed post Windows Server 2016


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: