User’s profile in PowerShell

0
155

If you often work with PowerShell, there is a chance that you have a set of the most common routines or prerequisites. Or maybe you like customization and you want to have your own, fancy PowerShell window. Today I’m going to show you the easiest way to do this.

In general, everything is very well described on the MS PS pages. But there is a big chance that you don’t want to try everything and you just need a working solution. In such case, we will create a file named profile.ps1 inside /Documents/WindowsPowerShell directory.

Create profile.ps1 inside C:/Users/UserName/Documents/WindowsPowerShell

The profile can contain many scripts written in PowerShell. They will be executed before the PS console will be opened. In my case, the file looks like this:

#Overrides standard prompt function. Used to determine system size (either 32 or 64 bits). 
function prompt {
#This information will be shown in the console title bar.
if ([System.IntPtr]::Size -eq 8) 
{$size = '64 bit'}
else 
{$size = '32 bit'}
#Gets current user and its role
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$secprin = New-Object Security.Principal.WindowsPrincipal $currentUser

if ($secprin.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator))
{
	$admin = 'Administrator'
}
else 
{
	$admin = 'non-Administrator'
}
#Prepares text displayed on console title bar
$host.ui.RawUI.WindowTitle = "$admin $size $(get-location)"
#Sets the text displayed before prompt marker
"TROLOLO> "
#Sets current directory to current working directory (PWD = path to the current working directory). 
#I don't remember when did I need it, it's a leftover you probably don't need.
[System.Environment]::CurrentDirectory = $PWD
#Creates a record of all or part of a PowerShell session to a text file in c:\temp folder
Start-Transcript c:\Temp\mytranscript.txt -append
} #end of prompt overriding
#loads newest version of the development module I frequently use. Set your own path here.
$cvModulePath = "C:\Repos\APP\io\CV.Management.PowerShell\bin\Debug\net48\CV.Management.PowerShell.dll"
Import-Module $cvModulePath

With this file in place, your console will look like in the picture below:

Difference between customized and non-customized console

But the main advantage of the presented profile is that all my commands + their outputs are stored safely in the temporary folder, and I can access them anytime, even after a year or so. This is especially useful during testing procedures.

Additionally, I don’t need to import the module that I use all the time, it’s loaded along with the console.

I encourage you to use this feature, probably you can also find more complex profiles on the net. Just start with this one and find if it suits you or maybe you need something more sophisticated 🙂

 

PS. If you don’t have Documents/WindowsPowerShell folder, just create it.

PS2. I strongly suggest changing „TROLOLO” to something else:) I personally use „PS”


Warning: A non-numeric value encountered in /home/platne/serwer17076/public_html/wp-content/themes/Newspaper/includes/wp_booster/td_block.php on line 1008