PowerShell for Windows Admins


April 29, 2019  12:36 PM

File rename

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

File rename is a topic that seems to keep recurring.

The simple answer is that you use Rename-Item

PS> Rename-Item -Path C:\test\Newoutdata01.txt -NewName OldData01.txt

 

If for whatever bizarre reason you have a character such as [ in your file name the rename won’t work

PS> Rename-Item -Path C:\test\New[outdata02.txt -NewName OldData02.txt
Rename-Item : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: New[outdata02.txt
At line:1 char:1
+ Rename-Item -Path C:\test\New[outdata02.txt -NewName OldData02.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Rename-Item], ParameterBindingException
+ FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.RenameItemCommand

 

The way round that is to use –LiteralPath

PS> Rename-Item -LiteralPath C:\test\New[outdata02.txt -NewName OldData02.txt

 

If you need to rename a bunch of files use Get-ChildItem and pipe the results into Rename-Item

April 27, 2019  9:09 AM

What’s new in PowerShell v6.2

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The What’s new in PowerShell v6.2 is available at – https://docs.microsoft.com/en-gb/powershell/scripting/whats-new/what-s-new-in-powershell-core-62?view=powershell-6 – together with the already existing documents for PowerShell v6.0 and v6.1.

Its worth reading through the three documents – one each for v6.0, v6.1 and v6.2 to see the whole range of changes in PowerShell core.


April 27, 2019  4:29 AM

CIM_Component class

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
CIM, Powershell

I saw a question about the CIM_Component class and wondered what it was. So I tried it

PS> Get-CimInstance -Namespace root\CIMV2 -ClassName CIM_Component | select -f 1 | fl *

GroupComponent : Win32_Directory (Name = “<directory path>”)
PartComponent : CIM_DataFile (Name = “<file path>”)
PSComputerName :
CimClass : root/CIMV2:CIM_DirectoryContainsFile
CimInstanceProperties : {GroupComponent, PartComponent}
CimSystemProperties : Microsoft.Management.Infrastructure.CimSystemProperties

What you’ll get is a list of every file on your system – showing folder in the GroupComponent (parent) and File in the PartComponent (child).

The documentation is totally accurate and totally useless as it explains that you get a parent-child relationship but not what’s in it!

I’d strongly recommend against using this class – there are easier ways to get the information.


April 20, 2019  10:46 AM

PowerShell version incompatibilities

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

There are incompatibilities between Powershell versions – you can’t use classes in Windows PowerShell v4 and there are differences between Windows PowerShell v5.1 and v6.x. One way to deal with PowerShell version incompatibilities is described in the recent post from the PowerShell team – https://devblogs.microsoft.com/powershell/using-psscriptanalyzer-to-check-powershell-version-compatibility/

I’m not a big fan of tools such as PSScriptAnalyzer as I find them restrictive and they get in the way of doing what I need to do. Having said that the usage described in the article is actually very useful especially the way you can configure which PowerShell versions you need to be worried about.

PSScriptAnalyzer is built into VSCode so the availability of the functionality to chaeck PowerShell version incompatibilities actually strengthens the case for using VSCode (I still don’t feel 100% comfortable with it – mainly because it does too much for what I need. The ISE is simpler and suits my needs better).

If you need work round PowerShell version incompatibilities in the code you write this may be of great help.


April 18, 2019  1:07 PM

File times

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

There are three pairs of file times that are available on files on Windows

PS> Get-ChildItem -Path C:\test\Newoutdata01.txt | select *time*

CreationTime      : 14/04/2019 17:28:41
CreationTimeUtc   : 14/04/2019 16:28:41
LastAccessTime    : 14/04/2019 17:28:41
LastAccessTimeUtc : 14/04/2019 16:28:41
LastWriteTime     : 25/02/2019 17:42:49
LastWriteTimeUtc  : 25/02/2019 17:42:49

This is how to modify those times

Use Get-ChildItem to iterate through the files. For each file use Set-Property to set the value on the appropriate property. The values I’m using are purely random – you’d normally use a known value.

The *Utc version of the properties will be automatically set based on its corresponding property

 


April 16, 2019  10:06 AM

Get wireless networks

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

I saw a question on how to get wireless networks which got me thinking about string handling and working with legacy command line utilities. I also wanted to compare the options available in Windows PowerShell and PowerShell Core.

First up is a relatively brute force approach. This approach works is relatively easy to understand and maintain but isn’t very elegant.

Start by using netsh to get the wireless networks. Skip the first 4 lines (run the netsh command by itself to see what’s being skipped) and filter out blank lines (empty strings). For each line check if it’s of interest and if so split on the : character and take the data into a variable. On the radio type line create an object and output.


April 14, 2019  10:41 AM

Brackets in file names

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

If you have brackets – [ or ] – in your file name you can see problems working with the file name. This is how to deal with brackets in file names.

Consider the files:

outdata01.txt
out[data02.txt
out]data03.txt
out]d[ata04.txt

which have a selection of brackets in the file name.

If you try something like

Get-ChildItem -Path c:\test | foreach {Rename-Item -Path $_.Fullname -NewName “New$($_.name)”}

You’ll find that two files

outdata01.txt
out]data03.txt
are successfully renamed but the other two fail with an error like

Rename-Item : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: out[data02.txt

Notice its the two files with left hand brackets that fail.

Its because PowerShell its treating the bracket as the opening of a wildcard / regex pattern.

The easiest way round this problem is to use –Literalpath instead of –Path

Get-ChildItem -Path c:\test | foreach {Rename-Item -LiteralPath $_.Fullname -NewName “New$($_.name)”}

And you’ll find all of the files rename successfully.

LiteralPath treats what ever its given literally – it doesn’t do the interpretation that Path attempts.

If you think you’re going to have odd characters in your file names then remember to use literalpath – you’ll find it as an alternative on all the core cmdlets that accept paths


April 11, 2019  8:26 AM

PowerShell 7

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

A recent post – https://devblogs.microsoft.com/powershell/the-next-release-of-powershell-powershell-7/ – on the PowerShell team blog states that the next release of PowerShell Core won’t be 6.3 as expected but will be PowerShell 7

PowerShell 7 will be tied to .NET Core 3.0 and should bring more compatibility for Windows users. The graphic on the post shows Linux usage outstrips Windows usage of PowerShell Core by at least 5 to 1.

The lifecycle will change to align more closely with .NET Core with Long Term Servicing releases and non-LTS releases.

PowerShell 7 will eventually ship side-by-side with Windows PowerShell 5.1 but update process hasn’t been finalised.

Terminology changes are in the offing as PowerShell core is dropped for just PowerShell. This is a mistake as it takes away the use of PowerShell as a generic term that covers all versions.

The reasons for the change aren’t fully explained in the post but I suspect are linked to the very poor level of take up of PowerShell Core on Windows.


March 31, 2019  3:13 PM

Pro Microsoft Hyper-V 2019

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Hyper-V, Powershell

I’ve always maintained that the most important thing about PowerShell is what you can do with it. This is brought out in my book – Pro Microsoft Hyper-V 2019 – https://www.apress.com/gb/book/9781484241158 .

My co-author and I describe how to create and manage a hyper-V environment. We use many PowerShell examples but they’re all in the context of getting your job done.

PowerShell is an important tool for administering your environment but at the end of the day remember the job is administration not fancy code. Your code should be good enough to get the job done and then move to the next task. You can always revisit code and polish it later.


March 30, 2019  2:45 PM

PSCommandNotFoundSuggestion

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

PowerShell v6.2 contains a number of experimental features including PSCommandNotFoundSuggestion

The idea of experimental features is to make the use of new features – especially those that may cause breaking changes optional.  Feedback can be obtainied and the experimental features can be modified, moved out of the experimental state into the full release or even removed.

Use

Enable-ExperimentalFeature  –Name PSCommandNotFoundSuggestion

to enable the feature. Then restart PowerShell.

Trying the new feature gave these results

PS> Get-Srvice

Suggestion [4,General]: The most similar commands are: Get-Service, Set-Service, New-Service, Get-PSDrive.

I expected Get-Service so that worked

Likewise

PS> Get-prcss

Suggestion [4,General]: The most similar commands are: Get-Process, Get-Alias, Get-Host, Get-Acl.

Looking for Get-Process – some of the other choices are very odd

Moving to native utilities

PS> pong

Suggestion [4,General]: The most similar commands are: popd, copy, move, ni, nv, oh, rni, rnp, sort, man.

I’d have expected to see ping in that list!

PS> Get-NtAdter

gives

Suggestion [4,General]: The most similar commands are: Get-NetAdapter, Set-NetAdapter, Get-Counter, Get-Item, Get-Date, Get-Member.

Expected Get-NetAdapter so that counts as a success.

Looking at aliases

PS> seloct

Suggestion [4,General]: The most similar commands are: select, sort, set, del, clc, sal, sl, sleep, start, sls.

PS> stv

Suggestion [4,General]: The most similar commands are: sv, stz, clv, ft, gpv, gv, nv, rv.

Needed to see select and stz respectively so that’s good.

One last utility

PS> ipconfog

Suggestion [4,General]: The most similar commands are: ipmo, ipcsv, ipconfig.exe, inmo.

ipconfig fits the bill.

For the most part the suggested commands seem to be reasonable and reasonably accurate.

Tab completion probably removes some of the usefulness of this option but I’d recommend enabling it and giving it a try


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: