PowerShell Profile

This week I have been at the PowerShell Usergroup in Munich, Germany.

They wished to get information about PowerShell Profiles, so I commited a talk on PowerShell Profile. And here is the content.

There are 4 PowerShell Profiles loaded on startup of the console. But there can be some more Profiles in your environment. That is, beacause you have two PowerShell profiles per PowerShell console. So VSCode, PowerShell ISE and the PowerShell command prompt have two profiles each. One Profile is for All Users, one for the current user. Additionally you have got a PowerShell Profile for Windows PowerShell and a seperate one for PowerShell Core. So there can be some PowerShell Profiles on your computer.

Use the following command in your prefered console to get the actual profile pathes:

  1. $profile.AllUsersAllHosts
  2. $profile.AllUsersCurrentHost
  3. $profile.CurrentUserAllHosts
  4. $profile.CurrentUserCurrentHost

By the way, they are loaded in exactly this order (which makes sense).

The 4 profiles you have in the running PowerShell ISE

What I have in my PSProfile

I talked about which PowerShell Profiles exists and also, what I have in my profile as an PowerShell automator. As an automator my focus is on execution duration, so the most relevant part is the execution time in the prompt. It looks like this in my console:

image

There are some more featues in my PSProfile prompt, eg the color of the provider is integrated in the path, for example FileSystem and registry:

image

You can find my Profile on Github.

PowerShell Profile Pathes

Here are some examples of profile pathes.

# PowerShell Core with VSCode
ProfilePathC:\Users\patrick\Documents\PowerShell\Microsoft.VSCode_profile.ps1

# Windows PowerShell with VSCode
ProfilePathC:\Users\patrick\Documents\WindowsPowerShell\Microsoft.VSCode_profile.ps1

# Windows PowerShell ProfilePath with ISE
C:\Users\patri\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

# Windosw PowerShell in the Console
C:\Users\patri\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Scripting Guy – Understanding the six PowerShell Profiles (there could be more, as you already know) 

Some ideas how to use the PowerShell Profile in a bad way

My Profile on github

First steps with VSCode (again)

Till now I did not use Visual Studio Code on a regular bases. Why? I always started using it for debugging my scripts, because I’ve been told, it is outstanding for debugging.
But it did not work for me. Each time something happend and I switched back to ISE.
Last week I open sourced my ISE profile – and than it happend, that I got a message from Tyler Leonhardt.

image

So now I simply have to have a better look on vs code. I did not have immediatly. Because now it is possible to position the powershell window on the right side (very important for me).

So I’ve had a look at Mike Robbins hints and the hints of Timothy Warner.

What I wanted

  • PowerShell like behavior – to start a script and code while the script runs
  • command window on the right side (for the second screen)
  • Nice icons
  • Autosave (I like that)
  • Wanted to try some autoformating

Now I started with the following mix:

The Extensions

  • Powershell (of course)
  • vscode-icons (nicer icons)
  • Markdown All In One (always good – with preview)
  • Azure Tools (I did not try this till now – but it looks impressing)

The Settings

I did a new mix of settings I like most. “powershell.integratedConsole.focusConsoleOnExecute” is the most relevant setting for me.

{
"workbench.iconTheme": "vscode-icons",
"window.zoomLevel": 0,
"editor.renderWhitespace": "all",
"editor.renderControlCharacters": true,
"files.trimTrailingWhitespace": true,
"files.defaultLanguage": "powershell",
"files.autoSave": "afterDelay",
"files.autoSaveDelay": 1000,
"editor.formatOnType": true,
"editor.formatOnPaste": true,
"powershell.integratedConsole.focusConsoleOnExecute": false,
"editor.mouseWheelZoom": true
}

Hope that helps you – this will be continued.

[Update 2018-10-30]
I got an advice, I’d like to mention here:
You may want to revise format on type format on paste and format on save. All good options but most of my issues resolve around those. I have reported them all. I use format on save only but i consider to disable it and even use format manually on request only.
See here
[Update 2018-11-21]
I followed the advice, because the VSCode broke really often. Now it is better.

[Update 2018-12-12 – Encoding]

The German Umlaute are mistyped in VSCode out of the box. For example when opening  a script, a ‘�’  appears instead of an ‘ä’.

The issue is closed on Github (see here). I tried the hint and set “files.autoGuessEncoding”: true – works for me now.

Did not set “files.encoding”: “windows1252” – because I never used this encoding before (till now it was UTF-8-BOM).

My favourite ISE profile

Currently I use Visual Studio code and PowerShell ISE at the same time.
I have had a nice ISE profile for a while now, which I’d like to introduce.

Welcome Message

Below you see the welcome message.

image

The welcome message includes:

  • actual User
  • actual Computer
  • Admin Modus
  • Datum / Uhrzeit

The Commandline

But the main advantage is in the command line itself. This looks like:

image

Here you can see at a glance:

  1. Time when the last command ended
  2. Execution time of last command humanized
  3. Admin mode (PS in red)
  4. PowerShell Provider (Green for FileSystem)

Where can you get it

The ISE Profile is on Github. https://github.com/pwahlmueller/ISE-Config

Does it loop? Foreach experiences with empty variable

Today I had some foreach fun in PowerShell. Does an empty variable loop? I would have said: No. But the correct answer is: It depends.

Let’s have a look.

<br>
$noValue -eq $null<br>

clip_image001

Ok, a previously unregistered variable is $null. But what can you do with foreach?

<br>
foreach ($oneValue in $noValue)<br>
{<br>
'Do not run.'<br>
}<br>

clip_image003

So a foreach with “$noValue” does not enter the loop code.

Now a slightly different foreach:

<br>
$noValue | ForEach-Object { 'Runs'}<br>

clip_image005

This time the loop code is executed. This one was a surprise for me.

But look further. What about an empty array? Let’s define one.

<br>
$noArray = @()<br>
foreach ($oneValue in $noArray)<br>
{<br>
'Do not run.'<br>
}<br>

clip_image007

It does not loop, too. And now the last one:

<br>
$noArray | % {'Runs'}  # % is equivalent to ForEach-Object<br>

clip_image008

This time, it also does not loop, as expected.

To avoid such inconvenience, initialize your variables at the start.

[Update 2019-03-14]
Today I got an answer from Bruce Payette, why this happens:

The foreach statement and ForEach-Object had identical, consistent behaviour in version 1 but users complained about the foreach statement iterating over null. As a result, we changed the behaviour in v2 (or v3 – I can’t remember which) adding a deliberate inconsistency.

PowerShell Praise

Some of my colleagues have told me that they receive too little praise. What have I done? I sent them this PowerShell script. And they liked it.

Here is the script ready to use:


Function Get-Praise

{

Param

(

   [switch]  $immediately

)

    Add-Type -AssemblyName System.Speech

    $praise = New-Object System.Speech.Synthesis.SpeechSynthesizer

    $praise.Speak('You are simply the best.')

}

Get-Praise -immediatly

Experts Live Café Wien–AWS and PowerShell Classes

Today we had two experts from Amazon Web Services (AWS) at our

Today we had our Experts Live Café in Vienna at HP Enterprise – and it was exciting as so often.

First Daniel Gruber introduced us to PowerShell classes, showed us the possibilities and limitations. Afterwards our guests from Seattle, Steve Roberts and Kenneth Hansen showed us the latest developments – some of them not even public – in the Amazon Cloud (AWS – Amazon Web Services).

20180618_155531289_iOS20180618_163157544_iOS

At the end we raffled a free ticket for the ExpertsLive conference among the participants, which Daniel Gruber won.

After the presentations we had intensive discussions. The two Americans also took notes about our feedback on AWS and its development – I think that one or the other will come to AWS or into the AWS PowerShell implementation.

All in all another successful evening. Our events are now on summer holidays – until the next event in October.

ExpertsLive Café in Linz sponsored by BASE IT

Yesterday we were guests at BASE IT in Ansfelden. We had 3 interesting presentations on the subject of automation. It is exciting to hear that System Center Service Manager is apparently becoming more and more popular and that there are now more and more people in Upper Austria who use it and are familiar with it.

Yesterday we had an automation focus, with these topics:
◾Phönix out of the ashes – Service Management and Automation with System Center (Michael Seidl, MVP)
◾Windows Admin Center RTM – formerly Project Honolulu (Markus Grudl)
◾Acitve Directory Domain Services Section (Patrick Grünauer, MVP)

This time we were a smaller – but very fine round – as our picture proves. There were many conversations – technical as well as private. Of the 8 participants 3 were MVP – a good cut, I think.

2018-06-13 ExpertsLiveCafé

The event was a really exciting.

On Monday, June 18, the next ExpertsLive event – this time in Vienna with Kenneth Hansen and Steve Roberts – they are working at Amazon/Seattle.

Sort string as int

Twice a year I have to sort a string as an integer. And with PowerShell it is really simple. What you have to do:

“199”,’23’,’89’ | Sort-Object

StringOrder

“199”,’23’,’89’ | Sort-Object -Property {$_ -as [int]}

CorrectOrder

I got this hint from Mathias R. Jessen. Have a look at his RegEx Session on PSConfEU. He also tells, what to do, if you have something like ‘IR343’ and ‘IR1342’ and you want to sort this numerical.

Your Error Code is too red?

So you think, the PowerShell Error-Code is too red?

image

It should be better green? There are ways.

<br>
$psISE.Options.ErrorForegroundColor = [System.Windows.Media.Colors]::Chartreuse<br>

or

<br>
$host.PrivateData.ErrorForegroundColor = ‚green‘

So error color will change to a more friendly green.

image

This runs on PowerShell ISE as well as on Visual Studio Code. I hope you’ll like it.

One more hint: Some people think, yellow is the best error color.