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
A Simple Introduction to Cisco CML2
0 3804 0Mark Jacob, Cisco Instructor, presents an introduction to Cisco Modeling Labs 2.0 or CML2.0, an upgrade to Cisco’s VIRL Personal Edition. Mark demonstrates Terminal Emulator access to console, as well as console access from within the CML2.0 product. Hello, I’m Mark Jacob, a Cisco Instructor and Network Instructor at Interface Technical Training. I’ve been using … Continue reading A Simple Introduction to Cisco CML2
Cable Testers and How to Use them in Network Environments
0 694 1This content is from our CompTIA Network + Video Certification Training Course. Start training today! In this video, CompTIA Network + instructor Rick Trader demonstrates how to use cable testers in network environments. Let’s look at some tools that we can use to test our different cables in our environment. Cable Testers Properly Wired Connectivity … Continue reading Cable Testers and How to Use them in Network Environments
Difference Between $_ and $PSItem in Windows PowerShell
2 1901 4In this video, I’m going to answer a very common question that I get when I’m teaching Windows PowerShell, and that’s, “What’s the difference between what’s called $_ and $PSItem?” Let me explain to you a little bit about what these two variables do. When we’re working with the PowerShell pipeline, and we want to … Continue reading Difference Between $_ and $PSItem in Windows PowerShell