PowerShell Profile? Which One?

Doctor Scripto

Summary: Ed Wilson, Microsoft Scripting Guy, talks about determining which Windows PowerShell profile to use for which functions. Microsoft Scripting Guy, Ed Wilson, is here. Living in central Florida can be a bit warm and humid in late July. Of course, I knew that before we moved here. I told one of my friends recently, when he commented on the heat and humidity, that it was truth in advertising. I told him if the Scripting Wife survived July and August here, she would know that she could handle the worst of the summer weather. Interestingly enough, on many days recently, it has actually been warmer back in Charlotte, North Carolina. Of course, the humidity and the UV Index have been higher here. But when one is talking about 95 degrees Fahrenheit, it really does not make that much difference. It’s not like we are going to be outside running a marathon at noon. But it is a great time to sit under the big ZZ Top tree in the back yard, and work on my new Windows PowerShell profile. I have also been making Gun Powder green tea, with hibiscus flowers, rose hips, and orange blossom honey, and then sticking it in the refrigerator. It is wonderfully refreshing, and it has just enough of a citrus flavor to it. It is my version of “southern iced tea.”

Use the right function in the right profile

There are differences between the Windows PowerShell ISE and the Windows PowerShell console host. This is obvious. There are also some things that I simply cannot do in the Windows PowerShell ISE. So when I am writing my Windows PowerShell profile, it makes sense to add some checks that make sure I am not trying to do something that I am not permitted to do. For example, I cannot start the Windows PowerShell transcript tool in the Windows PowerShell ISE because it does not support the transcript (at least not as of Windows PowerShell 4.0). Also, the Windows PowerShell ISE output pane does not support the pager…that is More. So when I output a multiple page output in the Windows PowerShell console, I can pipe the output to More, and I will see one page at a time. In the Windows PowerShell ISE, I don’t get an error, but I do not get paged output either. So, it makes sense to not even call these sorts of commands in the Windows PowerShell ISE. What I need is a good way to determine if I am running in the Windows PowerShell console, or if I am running in the Windows PowerShell ISE.

Test-ConsoleHost

I can create a simple function, called Test-ConsoleHost, that will determine if I am running in the Windows PowerShell console. To do this, I look at the Name property of the $host automatic variable. If it matches ConsoleHost, then I return True, otherwise I return False. Here is the function:

Function Test-ConsoleHost

{

 if(($host.Name -match ‘consolehost’)) {$true}

 Else {$false}  

} When I run load the function into the Windows PowerShell ISE, I can call the function directly. Because it returns $false if it is not in the Windows PowerShell console, it will return $false in the Windows PowerShell ISE. Here I check for the not true condition, and therefore, it returns the ISE name:

PS C:\> if(-not (Test-ConsoleHost)) {“ise”}

ise I add the Test-ConsoleHost function to my Windows PowerShell profile. I also create an alias for it called tch. This is shown here in my Windows PowerShell profile:

#Aliases

Set-Alias -Name ep -Value edit-profile | out-null

Set-Alias -Name tch -Value Test-ConsoleHost | out-null

#Functions

Function Edit-Profile

{ ISE $profile }

Function Test-ConsoleHost

{

 if(($host.Name -match ‘consolehost’)) {$true}

 Else {$false}  

}

#Variables

New-Variable -Name doc -Value “$home\documents”

#PS_Drives

New-PSDrive -Name Mod -Root ($env:PSModulePath -split ‘;’)[0] `

 -PSProvider FileSystem | out-null

#Commands

Set-Location c:\

Start-Transcript Now when I close the Windows PowerShell console and open it again, I can call the Test-ConsoleHost function directly via the alias. As shown here, it works:

PS C:\> tch

True Now I can wrap a bit of code around it, and use the Test-ConsoleHost function to get my Windows PowerShell host. As shown here, it also works:

PS C:\> if(tch) {“this is the powershell console”}

this is the powershell console

PS C:\> Now, if I want to, I can “safeguard” my Start-Transcript command by doing a Windows PowerShell host check:

If(tch) {Start-Transcript} That is all there is to adding functions to your Windows PowerShell console profile. Profile Week will continue tomorrow when I will talk about more cool stuff. I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace. Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon