Monthly Archive

Categories

Hidden files

If you suspect there are hidden files in a folder you can use the Attributes parameter to discover them:

PS> Get-ChildItem -Path c:\test -Attributes H

OR

PS> Get-ChildItem -Path c:\test -Attributes h

OR

PS> Get-ChildItem -Path c:\test -Hidden

 

If you want to see all files irrespective of them being hidden use Force

PS> Get-ChildItem -Path c:\test –Force

 

which will also show system files.

There are a number of ways to make a file hidden but the most generic is:

PS> Get-ChildItem -Path C:\test\Newoutdata03.txt | ForEach-Object {$_.Attributes += 'Hidden'}

 

You can modify the criteria used to define the files passed into Foreach-Object by using the path, filter, exclude or include parameters of Get-ChildItem. The Hidden attribute is set while preserving the other attributes.

To remove the Hidden attribute:

PS> Get-ChildItem -Path C:\test\ -Hidden | ForEach-Object {$_.Attributes -= 'Hidden'}

 

The Hidden attribute is removed whilst preserving other attributes. The files passed into ForEach-Object can again be filtered with the Get-ChildItem parameters.

Receive-Job Keep parameter

The Receive-Job Keep parameter is required if you want the data contained in the job to remain available. if you don’t use the –Keep parameter (a switch) the data will be deleted.

 

Its a pain to remember to use the Keep parameter. I’ve been working with jobs a lot just recently and the number of times I had to rerun jobs because I forgot to use Keep doesn’t bear thinking about.

 

I finally got round to adding it to the default parameter values.

This line in my profile

$PSDefaultParameterValues = @{'Install-Module:Scope'='AllUsers'; 'Update-Module:Scope'='AllUsers'; 'Receive-Job:Keep' = $true}

 

ensures I won’t forget ever again. Because Keep is a switch you have to set the value to true.

PowerShell Day UK 2019 slides and code

Had a great day at PowerShell Day UK 2019. The PowerShell Day UK 2019 slides and code for my session are available at https://github.com/RichardSiddaway/PSDay2019.

UK PowerShell day is Saturday

The UK PowerShell day is Saturday – 28 September  - two days away. If you want to be there tickets are still available - https://psday.uk/

 

If you’re going – say hello

 

See you there

Ternary operator

PowerShell v7 preview 4 adds a Ternary operator to PowerShell.

 

A ternary operator is a way to provide shortened coding for a simple if-else block. Its an operator that takes three operands rather than the usual two hence the name ternary.

for example

PS> $a = 5
PS> $b = 3
PS> if ($a -gt $b){'Greater'}else{'Not greater'}
Greater

 

Using a ternary operator this becomes

PS> $a -gt $b ? 'Greater' : 'Not greater'
Greater

 

The generic form is

<condition> ? <value if true> : <value if false>

 

The ternary operator is an experimental feature so needs to be enabled

PS> Enable-ExperimentalFeature PSTernaryOperator

 

and PowerShell restarted.

 

If I use the new operator I think its going to be when I’m working interactively. For scripts and modules I prefer to use the more verbose if statement as its easier to read and understand.

Get-ExecutionPolicy

I’ve used Get-ExecutionPolicy since PowerShell v1 and never stopped to think about it. The cmdlet normally returns just the current policy

PS> Get-ExecutionPolicy
RemoteSigned

 

However, if you dig a little deeper

PS> Get-ExecutionPolicy -List

Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine RemoteSigned

 

What you’re seeing is the LocalMachine policy.

 

You can use the Scope parameter on Set-ExecutionPolicy to create a more granular approach to execution policy if required

PowerShell v6.2.3

PowerShell v6.2.3 is available from https://github.com/PowerShell/PowerShell/releases

 

It fixes a debugging performance issue and updates the .NET SDK and runtime framework version.

 

Similar fixes are available in v6.1.6 released at the same time

Hyper-V VM IP address

Saw a question about getting the Hyper-V VM IP address.

 

One thing with Hyper-V is that the VM has to be running for you to retrieve the IP address.

 

Once you VM is running you can get the IP address

PS> Get-VM -Name W19ND01 | select -ExpandProperty NetworkAdapters | select VMname, Name, IPAddresses

 

You’ll get the IPv4 and IPv6 address returned

Pet peeves

Back in July I mentioned the using ? instead of Where-Object was a pet peeve. I’ve been asked a few times since for other pet peeves. The order of peevishness changes over time but these three will probably be always near the top.

 

In no particular order.

 

Peeve – the use of aliases in scripts. Aliases are fine for interactive use but when coding scripts or modules you should ALWAYS use the full cmdlet name and parameter names. If you use VScode or ISEsteroids you’ll find warning messages about the use of aliases. The problem with using aliases is that it makes the code harder to maintain especially for new comers. if you want to write impenetrable code that no one can read then switch to Perl. PowerShell has verb-noun cmdlet naming conventions and good (usually) parameter names for a reason. It makes the code easier to read and therefore understand and maintain. I guarantee that you’d rather inherit some code that uses full names rather than all aliases – been there, done that and its painful.

 

Peeve – the use of posh as an abbreviation for PowerShell. Its not needed and doesn’t add anything to the conversation. Just don’t.

 

Peeve – the use of multiple calls to Add-Member to create an object. You had to do this back in PowerShell v1. You should use New-Object or create a class to define your object. Its more efficient and easier to maintain. Don’t keep using outdated techniques just because there are still examples on the Internet.

 

I’ll publish more peeves sometime in the future.

Foreach-Object -parallel

The introduction of Foreach-Object -parallel in PowerShell v7 preview 3 brings some much needed parallelisation options back into PowerShell.

 

PowerShell workflows are available in Windows PowerShell but are quirky (to be kind) and can be difficult to use. Workflows were removed in PowerShell v6.0

 

In PowerShell v7 preview 3 Foreach-Object receives a –parallel parameter that takes a scriptblock as its value.

 

As a simple example consider this simple counting example from the PowerShell team blog:

PS> (Measure-Command -Expression {1..50 | ForEach-Object {Start-Sleep -Milliseconds 100}}).TotalSeconds
5.4960846

 

Now using the parallel option

PS> (Measure-Command -Expression {1..50 | ForEach-Object -Parallel {Start-Sleep -Milliseconds 100}}).TotalSeconds
2.2523929

 

The time to execute is significantly reduced.

 

A few caveats are needed.

Firstly, the parallel option is an experimental feature so must be enabled

Enable-ExperimentalFeature -Name PSForEachObjectParallel

 

Secondly, not all tasks are suitable candidates for parallel execution. Expect more discussion on this topic in the near future including contributions on the PowerShell team blog.