Monthly Archive

Categories

PowerShell

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

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

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

Windows Terminal v0.4.2382.0

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.

Test if a transcript is running

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 return if there is a transcript running and False if there isn’t.

Identifying the host

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

If 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

PowerShell Day UK 2019

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.