Archive for August, 2010

Editing Powershell in Visual Studio

Looking for Powershell support in Visual Studio 2010?? Check out PowerGUI VSX! Essentially you get the great features of PowerGUI inside the Visual Studio IDE. Well worth downloading.

Read more

Powershell Prompt

Customising my Powershell prompt was something that I never really thought I’d do. After all surely the out of the box prompt tells me where I am and making a new one seems like a bit of work. Well actually it’s not and I kept getting my x86 and x68, admin and non-admin prompts confused. So I altered my prompt to inlucde this information.

Firstly, to change your prompt you just need to write your own prompt function as this is what generates the prompt each time the prompt is printed to the screen. To see the default promtp try the following:

1
(Get-Item Function:\prompt).Definition

You’ll get back something like this:

1
2
$(if (test-path variable:/PSDebugContext) { '[DBG]: ' } else { '' }) + 'PS ' +
$(Get-Location) + $(if ($nestedpromptlevel -ge 1) { '>>' }) + '> '

So the first thing that I wanted to change was adding [Admin] in red to the start of the prompt if I’m running as an administrator. I have a global variable in my profile called $IsAdmin that tells me if I’m an admin or not. So the code below uses write-host to output ‘[Admin]‘ in red if I’m an Administrator.

1
2
3
4
if( ([System.Environment]::OSVersion.Version.Major -gt 5) -and ($IsAdmin) )
{
write-host '[Admin]' -NoNewLine -Fore 'red'
}

Next, I wanted to show if I was running x86 or x64. Which simply involved creating a string with the right architecture type and adding that to the prompt.

1
2
3
4
5
6
7
$bitness = ''
if ([IntPtr]::Size -eq <img src="https://web.archive.org/web/20120320084139im_/https://www.project84.net/wp-includes/images/smilies/icon_cool.gif" alt="8)" class="wp-smiley"> {
$bitness = '(x64)'
}
elseif ($Pscx:IsWow64Process) {
$bitness = '(x86)'
}

I added a few other bits but here’s the end result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
## Set My Prompt
## . Set-Prompt.ps1 in profile.
function prompt {
## If this is Vista or higher and we're an admin change the prompt
$adminPrompt=''
if( ([System.Environment]::OSVersion.Version.Major -gt 5) -and ($IsAdmin) )
{
#$adminPrompt='[Admin]'
write-host '[Admin]' -NoNewLine -Fore 'red'
}
## Get the commmand ID for Get-History/Invoke-History
$nextCommandId = (Get-History -count 1).Id + 1
#was there an error in the last thing we did??
$err = !$?
#Totally stole this from somewhere, finds the architecture
$bitness = ''
if ([IntPtr]::Size -eq <img src="https://web.archive.org/web/20120320084139im_/https://www.project84.net/wp-includes/images/smilies/icon_cool.gif" alt="8)" class="wp-smiley"> {
$bitness = '(x64)'
}
elseif ($Pscx:IsWow64Process) {
$bitness = '(x86)'
}
if($err) { $fg = "Red" } else { $fg = "Cyan" }
write-host "PS$bitness [$nextCommandId]$(Get-Location)>" -NoNewLine -Fore $fg
return " "
}
Read more

Powershell Console Colours

I’m certain this is probably elsewhere on the web but I couldn’t find it immediately. I was wondering how to ouput the names of each console colour in the corresponding colour. Turns out it’s really easy:

1
[consolecolor]::GetNames([consolecolor]) | %{Write-Host "$_" -For $_}
Read more