How to set up a PowerShell Profile

Keep Calm and Lean PowerShell

One of my biggest time savers every day is a PowerShell profile I have set up on my machine. Using your PowerShell profile you can save commonly used scripts/commands as functions, create your own shortcuts, or automate things you may do every time you open PowerShell, i.e. logging in to Office 365 Remote PowerShell!

Creating your Profile

Before you start editing your profile you need to create it using the following steps:

  1. On the computer that hosts the data warehouse management server, click Start, point to Programs, point to Windows PowerShell 1.0, right-click Windows PowerShell, and then click Run as administrator.
  2. At the Windows PowerShell prompt, type the following command, and then press ENTER:
    Test-path $profile
    
  3. If the results of the previous command are false, go to step 4. If the results are true, go to step 5.
  4. Type the following command, and then press ENTER.
    New-item –type file –force $profile
    
  5. Type the following command, and then press ENTER.
    Notepad $profile
    
  6. In the profile, type Add-PSSnapIn SMCmdletSnapIn. If you are adding this command to an existing profile, add it on a new line at the end of the profile.
  7. On the menu bar, click File, and then click Exit.
  8. In Notepad, click Save.
  9. Type the following commands, and then press ENTER after each command.
    Set-ExecutionPolicy RemoteSigned
    
Personalising your Profile

Once you have created your profile you can open it to edit by running the following command:

notepad.exe $Profile

Within the notepad file that opens you can add sections of code and common functions. In the example below, every time you open PowerShell it will greet you with the correct time of day, and ask whether you would like to sign in to Office 365 Remote Powershell:

$Hour = (Get-Date).Hour
If ($Hour -lt 12) {"Good Morning Mike"}
ElseIf ($Hour -gt 17) {"Good Eventing Mike"}
Else {"Good Afternoon Mike"}
$runningAzure = Read-Host "Are you connecting to Office 365? (Y/N)"
if($runningAzure -eq "Y"){

$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session
Connect-MsolService -Credential $livecred

}

And when you launch PowerShell, this is what this looks like:

Powershell

And if you enter Y, you will be shown a credential prompted and automatically logged in to Office 365! The possibilities are nearly endless, so go and try it out and see how much time you can save with a PowerShell profile!

Advertisements

One thought on “How to set up a PowerShell Profile

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s