PowerShell: long lines in Get-History

If I just save my history thus…

get-history | clip

, then I find beyond 80 characters whatever, you get truncation (note the ellipses):


Outputting to json helps…

Get-History | ConvertTo-Json

, but it is still a bit of a hunt to find what you want:


First, we will get the history into an array of strings, splitting on Newline:


$history = Get-History | ConvertTo-Json
$historyLines = $history -split [System.Environment]::Newline

From the previous screenshot, I am only interested in those lines which contain [CommandLine]:

$historyLines | % {if ($_ -match "CommandLine") { $_ }}


But I want to exclude the repeated “CommandLine” etc. For now, I’ll just cheat. I found that the start of the variable part is at offset 25, so I’ll just do this for now:

$historyLines | % {if ($_ -match "CommandLine") {$_.substring(25, $_.length - (25+2)) }}

, which gives this, which is good enough for now:


Finally, bringing the 3 lines together:

$history = Get-History | ConvertTo-Json
$historyLines = $history -split [System.Environment]::Newline
$historyLines | % {if ($_ -match "CommandLine") {$_.substring(25, $_.length - (25+2)) }}


Aliases, plus other ways to get the full history

h | Format-List -Property *
h | Format-List -Property CommandLine
h -id 31 -Count 10
h -id 31 -Count 10 | fl
h | fl -Property CommandLine | clip


Azure: more than 1 subscription?

Yes, I do have more than one.

But before getting to that, when you start a PowerShell Azure session, login like this (in a “proper” environment you might have a secured credentials file):


This is the THE way to log in. Just do it. And notice the presence of RM for Resource Manager. Where there is a “with-RM” option, always take that, as alternative verb-nouns which do not include RM will tend to refer to the so-called “Classic” Azure components, aka Legacy, aka deprecated.

Moving on… I have more than 1 subscription:


I found that no permutation of Set-AzureSubscription actually set my subscription to the requested one, even though it claimed it had. For me at least, only this worked:


So, got that?
Login to your account: [AddAzureRmAccount]
Set your subscription to the required one: [Set-AzureRmContext]


And while on the basics, if PowerShell fails to recognise these commands, then run

Install-Module AzureRM

Install-Module Azure

to pick up both Resource Manager and Classic modules.

 Later… turns out that Login-AzureRmAccount is just an alias for AddAzureRmAccount

PowerShell: The basic template in ISE

The basic Ctrl-J template in the PowerShell ISE is a bit noisier than I need. This is all I want:


Drop set of databases
Drop-Database -DatabasePrefix “TestSet”
function Drop-Database
Param (
Begin {
Process {
End {

Beyond that, you can use the New-IseSnippet function to make it part of the ISE, and use Ctrl-J to invoke it. Straight from msdn…

$m = @'

PS C:\>New-ISESnippet -Text $m -Title Mandatory -Description "Adds a mandatory function parameter." -Author "Kim Akers, Fabrikam Corp." -Force

So applying that principle to our very basic snippet at the top, we get..

$m = @'
<# .Synopsis Drop set of databases .Example Drop-Database -DatabasePrefix "TestSet" #>
function Drop-Database {
Param (
Begin {
Process {
End {

New-ISESnippet -Text $m -Title DropDatabases -Description “A snippet to drop databases.” -Author “Dennis Wells, The Things I Do” -Force

Not so fast… So this was OK to execute…



But then…


Exception calling “Load” with “1” argument(s): “Unable to find or access the file C:\Users\denni\Documents\WindowsPowerShell\Snippets\DropDatabases.snippets.ps1xml”
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\ISE\ISE.psm1:102 char:9
+ $psise.CurrentPowerShellTab.Snippets.Load($params[“FilePath”] …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException

Looking at the file location, the offending file is clearly there…


, with this content:


Executing the command line a second time also gives the error.

Hang on…

I recently rebuilt this machine.

If we try this…

New-Item -ItemType File -Path $profile -Force


And from then on just refer to my old post here




, and as admin..


Now we shall try again…


That means I expect to find a snippet with a title of [Mandatory] when I press Ctrl-J in the ISE.


Hm, so that all works, but it doesn’t seem keen on the carats in the $<parametername>. Fine, don’t care.

I won’t bother with the evidence, but following that little trial, I then ran the DropDatabases snippet through the New-ISESnippet command, and it also appears in the selection list, and can be invoked.

No matter how irritating it is to get these automation-related things working, they SO pay you back for the effort.

PowerShell: change default prompt

reallThanks here



Now PS has setup the $profile file in the right location, edit in Notepad:


function Global:prompt {“PS [$Env:username]$PWD`n>”}



Oh yes, this is a new machine…



Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

(and remember you have to run this as admin)

I’d say that’s a pretty useful prompt – the backtick gives you back your real estate once you know the directory name – thanks StackOverflow person