PowerShell for Windows Admins


March 30, 2019  6:48 AM

PowerShell v6.2 experimental features

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

I’ve mentioned the PowerShell v6.2 experimental features before.

This blog post from the PowerShell team https://devblogs.microsoft.com/powershell/general-availability-of-powershell-core-6-2/

gives a good overview of the available experimental features.

I’ve already covered the use of the temp drive. The command not found suggestions and implicit remoting batching look like they could be useful.  The abbreviation expansion could make interactive use more efficient

March 30, 2019  6:17 AM

Copy-Item

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Copy-Item seems to cause problems for many users.

I’ve written an article on the use of Copy-Item that you may find useful – https://searchwindowsserver.techtarget.com/tip/PowerShell-commands-to-copy-files-Basic-to-advanced-methods


March 28, 2019  1:45 PM

PowerShell v6.2 release

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The Powershell v6.2 release has just been made available on github – https://github.com/PowerShell/PowerShell/releases

The full release notes aren’t available on the Microsoft documentation yet – they should appear in the What’s New section of https://docs.microsoft.com/en-us/powershell/scripting/overview?view=powershell-6

The release notes on github indicate only one breaking change  – around the –NoEnumerate behaviour in Write-Output

There are some relatively minor cmdlet updates and fixes – nothing leaps out as a major issue.

You have six months to upgrade to v6.2 before the v6.1 support stops


March 28, 2019  8:36 AM

Read-Host

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Read-Host enables you to prompt for input for your functions and scripts. For instance I see a lot of examples of this sort of code:

$group = Read-Host -Prompt ‘Group Name’
Get-ADGroupMember -Identity $group

You should only use Read-Host as a last resort. The correct way to pass data into scripts and functions is by parameters. You can create a param() block in a script!

function Get-Members {
param (
[string]$group
)
Get-ADGroupMember -Identity $group
}

Use the function as:

Get-Members -group finance

The other advantage of parameters is that you do lots of clever validation on the values.

So, stop using Read-Host and use parameters – you know you want to.


March 25, 2019  5:27 AM

Powershell default parameters

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The issue Scope issue with Install-Module that I discussed in recent posts is annoying because I know that I’ll forget about it and end up installing in the wrong place and have to uninstall and reinstall. Then I remembered PowerShell default parameters.

By adding the line

$PSDefaultParameterValues = @{‘Install-Module:Scope’=’AllUsers’}

into my PowerShell v6 profile I’ve removed the issue. Install-Module will always default to –Scope AllUsers and if I need to override it I can.

$PSDefaultParameterValues is a hashtable where the key is constructed from the cmdlet name and the parameter and  a value is supplied.

I use the same profile for production and preview PowerShell v6 instances so all should be good – until there’s another change.


March 22, 2019  5:11 AM

Putting user information into computer description

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

I was recently asked how to add user information – specifically first and last name – into  computer description in Active Directory.

 

First get your user

$user = Get-ADUser -Identity FredBrown

 

Then add the information to the computer’s description

Set-ADComputer -Identity W10ProIp -Description “Used by $($user.Givenname) $($user.Surname)”

 

You need to use the subexpression – $() – syntax to resolve the values rather than getting references to the $user object.

Then test the description has been set

Get-ADComputer -Identity W10ProIp -Properties  Description

 

if it was me I’d add the samAccountName or other unique identifier as name isn’t sufficient to uniquely identify the user


March 21, 2019  11:59 AM

Install-Module in PowerShell v6.2 RC 1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Further to my last post it appears that Install-Module in PowerShell v6.2 RC1 DOESN’T follow the rules. In an elevated session if the scope parameter ISN’T used it will install to $home\Documents\PowerShell\Modules

The default appears to be CurrentUser regardless.

You have to use -Scope AllUsers if you want it to install in C:\Program Files\PowerShell\Modules


March 21, 2019  11:16 AM

Install-Module Scope parameter

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Be aware of the Scope parameter when using Install-Module. The following rules apply:

Allusers scope installs to $env:ProgramFiles\PowerShell\Modules

CurrentUser scope installs to $home\Documents\PowerShell\Modules

NoScope defined:

– – For an elevated PowerShell session, Scope defaults to AllUsers

– –  For non-elevated PowerShell sessions in PowerShellGet versions 2.0.0 and above, the Scope is CurrentUser

– – For non-elevated PowerShell sessions in PowerShellGet versions 1.6.7 and earlier, Scope is undefined, and Install-Module fails

 

PowerShell v6.2 RC 1 uses PowerShellGet 2.0.4 and PowerShell v6.1.3 uses version 1.6.7. You’ll see different behavior depending on PowerShell version so the advice is to use the Scope parameter to ensure the module goes where you intend.

 

I usually want my modules in Program files (Allusers) rather than my documents folder (CurrentUser).

 

The lesson seems to be to use the Scope parameter  rather than relying on defaults


March 9, 2019  6:46 AM

PSTempDrive

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The release of PowerShell v6.2 release candidate 1 brings more experimental features including PSTempDrive.

You can view the currently available experimental features using get-ExperimentalFeature.  You’ll find a total of four:

PSCommandNotFoundSuggestion
PSImplicitRemotingBatching
PSTempDrive
PSUseAbbreviationExpansion

Install PSTempDrive using

Enable-ExperimentalFeature –Name PSTempDrive

Restart PowerShell after enabling the feature.

Get-PSDrive will show a new drive named  Temp. The root of the drive is set by the path in your TEMP environmental variable.

You can use and access the TEMP drive like any other drive set by PowerShell.

The TEMP drive follows the pattern of other drives created from a path on an existing drive using the filesystem provider in that the used and free space figures reflect the situation for the whole volume not the individual drives.  The free space is OK like this as theoretically you could consume the whole of the available space on your new drive but the used space should reflect reality.  The C: drive used space should be for the whole volume but the TEMP: drive should only show the space used in your TEMP folder etc.

If you want to remove the experimental feature – use Disable-ExperimentalFeature and restart PowerShell.

Not wholly convinced of the need for this particular feature but it gives marginally easier access to the TEMP folder.

Remember that experimental features are just that – experimental – and could be modified or even removed in a later version of PowerShell


March 6, 2019  9:09 AM

PowerShell v6.2 release candidate 1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

PowerShell v6.2 release candidate 1 is available – https://github.com/PowerShell/PowerShell/releases

The only breaking change is to how Join-String works in a non-pipeline scenario.  That shouldn’t be a big issue as Join-String is new to v6.2.

The security fixes from v6.1..3 have been incorporated in the release candidate.

Experimental features gets a couple of new options round creating a TEMP:\ drive and suggestions given when a command isn’t found.


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: