Friday, May 20, 2016

Enable Preview of ps1 files in Windows Explorer


One liner, works instantly, no restart of explorer required.

Set-ItemProperty Registry::HKEY_CLASSES_ROOT\.ps1 -Name PerceivedType -Value text

Monday, May 16, 2016

Text manipulation


Replace, Remove

A simple character removal from a string
$SapSName = "El-Asswi"
If ($SapSName -like "*-*")
        {
        $SapSName = $SAPSName.Replace("-","")
        }
$SapSName


The following manipulates this $DeploymentTypeRef


<DeploymentTypeReference AuthoringScopeId="ScopeId_AA0BA009-8B99-4F9F-A54D-4E5F3F43F0FA" LogicalName="DeploymentType_a1c9acc4-a0fa-4f79-b0a0-b0da75418f9d" Changeable="true" />

into this $DeploymentTypeRefExtract

ScopeId_AA0BA009-8B99-4F9F-A54D-4E5F3F43F0FA/DeploymentType_3d3239d1-e3dd-4be0-bcbf-cde7e0a84c7f



$DeploymentTypeRefExtract = $DeploymentTypeRef.Remove(0,61).Replace(""" LogicalName=","").Replace("""DeploymentType","/DeploymentType").Replace(""" Changeable=","").Replace("true"" />","").Replace("""","")

Thursday, May 12, 2016

out-file

out-file is brilliant.  It is so helpful to get stuff out into txt files both for logging what has happened but also for helping you during the script design to understand what is  going on.

The best why to do it is to create a the variable which specifies the output file name.  You can even use variables to create the variable which can be very useful.

During testing it is useful to write stuff out to public which is always successful regardless of the user context the script is running it.


$OutputFile = $env:PUBLIC+"\Documents\"+$AnotherVar+".txt"

command | out-file $OutputFile

Thursday, May 5, 2016

Use of NULL


$null is available to represent nothing.  You can use this if you need to check for nothing, or indeed, "not nothing" i.e. something.  This command is an example of its use.  In this case it only returns items where the ExcludeFileList value in not blank or null.

Get-WmiObject -Namespace ROOT\ccm\SoftMgmtAgent -Class CacheInfoEx | where {$_.excludefilelist -notlike $null}

Wednesday, May 4, 2016

Customise the powershell prompt

When you're happily typing powershell commands all day long, you may want to check when you executed a particular command.

With this little bit of code you can configure the prompt to record the day, time and location so that the powershell window can be a record of when each command was run.

One liner !

function prompt {$(get-date -UFormat %d/%m) + " " + $(get-date -UFormat %H:%M) + " " + $(get-location) +" > "}