So, what’s a PowerShell $Profile got to do with it?
So, what’s a PowerShell $Profile got to do with it?
So, students ask me about my PowerShell $profile that I use in class. They want to know how I got the line numbering, and how I retrieve history commands and re-execute them so quickly
Well, among other things, I added a couple of functions to my $profile that help me out when I’m presenting. First, for my prompt. I didn’t invent the wheel on this, I actually took a look at the help file about_prompt and modified one the examples.
I wanted the prompt to stand out on the console display more, so I threw in the
$Host.UI.Write("Yellow",$Host.UI.RawUI.BackGroundColor,"[PS:$nextCommand]")
1 | <span style="color: #0000ff;">function</span><span style="color: #0000cd;">prompt</span><span style="color: #000000;">{ </span><span style="color: #800080;">$history</span><span style="color: #ff0000;">=</span><span style="color: #000000;">@</span><span style="color: #000000;">(</span><span style="color: #5f9ea0;">get-history</span><span style="color: #000000;">) </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(</span><span style="color: #800080;">$history</span><span style="color: #000000;">.</span><span style="color: #8b4513;">Count</span><span style="color: #ff0000;">-gt</span><span style="color: #000000;">0</span><span style="color: #000000;">) { </span><span style="color: #800080;">$lastItem</span><span style="color: #ff0000;">=</span><span style="color: #800080;">$history</span><span style="color: #000000;">[</span><span style="color: #800080;">$history</span><span style="color: #000000;">.</span><span style="color: #8b4513;">Count</span><span style="color: #5f9ea0;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">] </span><span style="color: #800080;">$lastId</span><span style="color: #ff0000;">=</span><span style="color: #800080;">$lastItem</span><span style="color: #000000;">.</span><span style="color: #8b4513;">Id</span><span style="color: #000000;"> } </span><span style="color: #800080;">$nextCommand</span><span style="color: #ff0000;">=</span><span style="color: #800080;">$lastId</span><span style="color: #ff0000;">+</span><span style="color: #000000;">1</span><span style="color: #008000;">#$currentDirectory = get-location </span><span style="color: #008000;">#"$nextCommand[PS] $currentDirectory>" </span><span style="color: #000080;">$Host</span><span style="color: #000000;">.</span><span style="color: #8b4513;">UI</span><span style="color: #000000;">.</span><span style="color: #8b4513;">Write</span><span style="color: #000000;">(</span><span style="color: #800000;">"Yellow"</span><span style="color: #000000;">,</span><span style="color: #000080;">$Host</span><span style="color: #000000;">.</span><span style="color: #8b4513;">UI</span><span style="color: #000000;">.</span><span style="color: #8b4513;">RawUI</span><span style="color: #000000;">.</span><span style="color: #8b4513;">BackGroundColor</span><span style="color: #000000;">,</span><span style="color: #800000;">"[PS:$nextCommand]"</span><span style="color: #000000;">) </span><span style="color: #800000;">" "</span><span style="color: #000000;"> }</span> |
The cool part is using Get-History to help me out. I wanted to use a fast-to-type alias that would retrieve and display a previous line number from the function above. I wanted to be able to display it, invoke it, or just copy it to my clipboard so I can paste it into an email or something. (I have the console edit and paste because multiple lines are a bitch to copy.)
So, I think the interesting part is using clip.exe to copy it to the clipboard. Try it out and see what you think!
1 | <span style="color: #0000ff;">function</span><span style="color: #0000cd;">Get-CommandLine</span><span style="color: #000000;"> { [</span><span style="color: #0000ff;">CmdletBinding</span><span style="color: #000000;">(</span><span style="color: #0000ff;">DefaultParametersetName</span><span style="color: #ff0000;">=</span><span style="color: #800000;">'</span><span style="color: #800000;">Id</span><span style="color: #800000;">'</span><span style="color: #000000;">)] </span><span style="color: #0000ff;">param</span><span style="color: #000000;">( [</span><span style="color: #0000ff;">Parameter</span><span style="color: #000000;">(</span><span style="color: #0000ff;">Position</span><span style="color: #ff0000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">,</span><span style="color: #0000ff;">ParameterSetName</span><span style="color: #ff0000;">=</span><span style="color: #800000;">'</span><span style="color: #800000;">Id</span><span style="color: #800000;">'</span><span style="color: #000000;">,</span><span style="color: #0000ff;">ValueFromPipelineByPropertyName</span><span style="color: #ff0000;">=</span><span style="color: #0000ff;">$true</span><span style="color: #000000;">,</span><span style="color: #0000ff;">Mandatory</span><span style="color: #ff0000;">=</span><span style="color: #0000ff;">$true</span><span style="color: #000000;">)] [</span><span style="color: #0000ff;">Parameter</span><span style="color: #000000;">(</span><span style="color: #0000ff;">Position</span><span style="color: #ff0000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">,</span><span style="color: #0000ff;">ParameterSetName</span><span style="color: #ff0000;">=</span><span style="color: #800000;">'</span><span style="color: #800000;">Clip</span><span style="color: #800000;">'</span><span style="color: #000000;">,</span><span style="color: #0000ff;">ValueFromPipelineByPropertyName</span><span style="color: #ff0000;">=</span><span style="color: #0000ff;">$true</span><span style="color: #000000;">,</span><span style="color: #0000ff;">Mandatory</span><span style="color: #ff0000;">=</span><span style="color: #0000ff;">$true</span><span style="color: #000000;">)] [</span><span style="color: #0000ff;">Parameter</span><span style="color: #000000;">(</span><span style="color: #0000ff;">Position</span><span style="color: #ff0000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">,</span><span style="color: #0000ff;">ParameterSetName</span><span style="color: #ff0000;">=</span><span style="color: #800000;">'</span><span style="color: #800000;">Invoke</span><span style="color: #800000;">'</span><span style="color: #000000;">,</span><span style="color: #0000ff;">ValueFromPipelineByPropertyName</span><span style="color: #ff0000;">=</span><span style="color: #0000ff;">$true</span><span style="color: #000000;">,</span><span style="color: #0000ff;">Mandatory</span><span style="color: #ff0000;">=</span><span style="color: #0000ff;">$true</span><span style="color: #000000;">)] [</span><span style="color: #0000ff;">string</span><span style="color: #000000;">]</span><span style="color: #800080;">$Id</span><span style="color: #000000;">,</span><span style="color: #000000;"> [</span><span style="color: #0000ff;">Parameter</span><span style="color: #000000;">(</span><span style="color: #0000ff;">Position</span><span style="color: #ff0000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #0000ff;">ParameterSetName</span><span style="color: #ff0000;">=</span><span style="color: #800000;">'</span><span style="color: #800000;">Clip</span><span style="color: #800000;">'</span><span style="color: #000000;">,</span><span style="color: #0000ff;">Mandatory</span><span style="color: #ff0000;">=</span><span style="color: #0000ff;">$false</span><span style="color: #000000;">)] [</span><span style="color: #0000ff;">switch</span><span style="color: #000000;">]</span><span style="color: #800080;">$Clip</span><span style="color: #000000;">,</span><span style="color: #000000;"> [</span><span style="color: #0000ff;">Parameter</span><span style="color: #000000;">(</span><span style="color: #0000ff;">Position</span><span style="color: #ff0000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #0000ff;">ParameterSetName</span><span style="color: #ff0000;">=</span><span style="color: #800000;">'</span><span style="color: #800000;">Invoke</span><span style="color: #800000;">'</span><span style="color: #000000;">,</span><span style="color: #0000ff;">Mandatory</span><span style="color: #ff0000;">=</span><span style="color: #0000ff;">$false</span><span style="color: #000000;">)] [</span><span style="color: #0000ff;">switch</span><span style="color: #000000;">]</span><span style="color: #800080;">$Invoke</span><span style="color: #000000;"> ) </span><span style="color: #0000ff;">process</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">switch</span><span style="color: #000000;">(</span><span style="color: #800080;">$PsCmdlet</span><span style="color: #000000;">.</span><span style="color: #8b4513;">ParameterSetName</span><span style="color: #000000;">) { </span><span style="color: #800000;">'</span><span style="color: #800000;">Id</span><span style="color: #800000;">'</span><span style="color: #000000;"> { </span><span style="color: #5f9ea0;">Get-History</span><span style="color: #5f9ea0;">-Id</span><span style="color: #800080;">$Id</span><span style="color: #0000ff;">|</span><span style="color: #0000ff;">select</span><span style="color: #5f9ea0;">-ExpandProperty</span><span style="color: #0000ff;">CommandLine</span><span style="color: #0000ff;">;</span><span style="color: #0000ff;">break</span><span style="color: #000000;"> } </span><span style="color: #800000;">'</span><span style="color: #800000;">Clip</span><span style="color: #800000;">'</span><span style="color: #000000;"> { </span><span style="color: #5f9ea0;">Get-History</span><span style="color: #5f9ea0;">-Id</span><span style="color: #800080;">$Id</span><span style="color: #0000ff;">|</span><span style="color: #0000ff;">select</span><span style="color: #5f9ea0;">-ExpandProperty</span><span style="color: #0000ff;">CommandLine</span><span style="color: #0000ff;">|</span><span style="color: #0000ff;">clip</span><span style="color: #0000ff;">;</span><span style="color: #0000ff;">break</span><span style="color: #000000;"> } </span><span style="color: #800000;">'</span><span style="color: #800000;">Invoke</span><span style="color: #800000;">'</span><span style="color: #000000;"> { </span><span style="color: #5f9ea0;">Invoke-Expression</span><span style="color: #000000;"> (</span><span style="color: #5f9ea0;">Get-History</span><span style="color: #5f9ea0;">-Id</span><span style="color: #800080;">$Id</span><span style="color: #0000ff;">|</span><span style="color: #0000ff;">select</span><span style="color: #5f9ea0;">-ExpandProperty</span><span style="color: #0000ff;">CommandLine</span><span style="color: #000000;">) } } } }</span> |
1 | <span style="color: #5f9ea0;">New-Alias</span><span style="color: #0000ff;">gcl</span><span style="color: #5f9ea0;">Get-CommandLine</span> |
Knowledge is PowerShell,
Jason
You May Also Like
Agile Methodology in Project Management
0 166 0In this video, you will gain an understanding of Agile and Scrum Master Certification terminologies and concepts to help you make better decisions in your Project Management capabilities. Whether you’re a developer looking to obtain an Agile or Scrum Master Certification, or you’re a Project Manager/Product Owner who is attempting to get your product or … Continue reading Agile Methodology in Project Management
JavaScript for C# Developers – September 24, 2014
0 495 3Is JavaScript worth taking the time to learn if I’m a server-side .NET developer? How much of C# carries over to JavaScript? In this recorded video from Dan Wahlin’s webinar on September 24,2014, Dan answers these questions and more while also discussing similarities between the languages, key differences, and the future of JavaScript (ES6). If … Continue reading JavaScript for C# Developers – September 24, 2014
How to clone a Windows Server 2012 or 2012 R2 Domain Controller
3 1634 3One of the coolest new features in Window Server 2012 and Windows Server 2012 R2 is the ability to clone a Domain Controller. In the past, if we had virtualized Domain Controllers and we actually took a snapshot of it and then rolled back to that snapshot, it would break the logon service on that … Continue reading How to clone a Windows Server 2012 or 2012 R2 Domain Controller