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.

Advertisements
Posted in Powershell | Leave a comment

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.

Posted in Powershell | Leave a comment

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.

Posted in Powershell | Leave a comment

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

Posted in Powershell | Leave a comment

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.

Posted in PowerShell 7 | 1 Comment

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

Posted in Powershell | Leave a comment

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

Posted in PowerShell v6 | Leave a comment