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 184 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
An Overview of Office 365 – Administration Portal and Admin Center
0 944 3This is part 1 of our 5-part Office 365 free training course. In this Office 365 training video, instructor Spike Xavier introduces some of the most popular services found in Microsoft Office 365 including the Admin Portal and Admin Center. For instructor-led Office 365 training classes, see our course schedule: Spike Xavier SharePoint Instructor – … Continue reading An Overview of Office 365 – Administration Portal and Admin Center
Creating Users and Managing Passwords in Microsoft Office 365
0 787 4In this Office 365 training video, instructor Spike Xavier demonstrates how to create users and manage passwords in Office 365. For instructor-led Office 365 training classes, see our course schedulle: Spike Xavier SharePoint Instructor – Interface Technical Training Phoenix, AZ 20347: Enabling and Managing Office 365