Categories
Design

How to create a powershell profile

@taybgibb
November 10, 2012, 2:00am EDT

How to create a powershell profile

PowerShell is a great way to automate almost anything in Windows. However, its not just a scripting language. If you find yourself using it as a command line shell it may be useful to store your functions and customizations in a profile that gets loaded every time you load the Console. Read on to find out how.

Creating a PowerShell Profile

The first thing we need to do is check if you already have a profile. There is an automatic variable, $Profile, that stores the fully qualified location of the PowerShell profile. An easy way to check if your profile exists is to use the Test-Path cmdlet on the $Profile variable.

How to create a powershell profile

As you can see we don’t have a profile file yet, so we will need to create one, you can easily do that with the New-Item cmdlet.

New-Item –Path $Profile –Type File –Force

Note: Using the force parameter will cause a profile to be created even if you already had one. This means your old profile will be overwritten.

How to create a powershell profile

You can edit your profile using notepad, which is easily started using PowerShell.

How to create a powershell profile

You can put any commands,functions,alias’s and even module imports into your PowerShell profile. Here are some things examples and ideas of what you can put in your profile.

Since PowerShell 3 comes with updatable help, the easiest way to keep your help files updated is by adding the Update-Help cmdlet to your profile.

Note: Update-Help will only download help files once a day, this is fine for us since we don’t want it updating help files every single time we open the console. If you do want it to update every time you can use the force parameter.

How to create a powershell profile

Another thing I like to add to my profile is custom functions that I have written over time, this makes them automatically available in the console. Below you can see that you can literally just copy a function out of a script and put it in your profile. It will then be available for use in the console.

How to create a powershell profile

Finally, I also have some customizations to the console. One of my favorite ones is shown below, it basically determines if you have opened an elevated PowerShell console and changes the font color, this way I always remember that I am running with elevated privileges.

How to create a powershell profile

What do you have in your profile? Let us know in the comments.

A PowerShell profile is a PowerShell script that runs automatically when a new PowerShell session is started. PowerShell profiles can be used to configure your PowerShell environment the way you like it, or to load custom functions for use in your PowerShell administration tasks.

There are actually six PowerShell profiles relating to the PowerShell console and the PowerShell ISE. You can see the four console-related profiles by running the following command:

The two additional ISE-related profiles are:

  • Current User, Current Host – ISE: $Home\[My]Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
  • All Users, Current Host – ISE: $PsHome\Microsoft.PowerShellISE_profile.ps1

For the purposes of this article I will be referring to the Current User, Current Host profile.

You can check for an existing PowerShell profile by using Test-Path.

If there is no existing profile create one with New-Item.

You can now edit the profile script using the PowerShell ISE or your PowerShell editor of choice and add custom functions and any other customizations that you require for your PowerShell console sessions.

Examples:

Paul is a Microsoft MVP for Office Apps and Services and a Pluralsight author. He works as a consultant, writer, and trainer specializing in Office 365 and Exchange Server.

Comments

Thank you for this! Most everywhere else has the insufficient suggestion of >> $profile | Get-Member -MemberType NoteProperty

I find the Powershell Profile to be very helpful in Exchange. Because the use of the Exchange Shell is getting more and more prevalent, I need to keep a historical record of what I ran and when. For this, I use the start-transcript as the first line in my PowerShell Profile.
I have several other entries, but I have a question:

How can I edit/modify/create a profile on all the servers in my domain? I hate having to log onto all the servers individually and manually create a powershell profile on each server and then it’s only for me…
Is there a way to automate this at all?

Yep. Note the part of the article above that mentions the other profile types that are available.

PowerShell is a handy profile to execute customized command lines in one click. It allows you to customize the environment that you want to open it.

A PowerShell profile is a script that runs when PowerShell starts. You can use the profile as a logon script to customize the environment. You can add commands, aliases, functions, variables, snap-ins, modules, and PowerShell drives. You can also add other session-specific elements to your profile so they are available in every session without having to import or re-create them.

PowerShell features are not available out of the box. Due to some security measures, the use of scripts is disabled. However, you can create a PowerShell profile on Windows 10 in easy steps.

How To Create A PowerShell Profile On Windows 10

The process of creating a PowerShell in Windows 10 is a pretty simple process. Follow the below steps.

  1. Open the PowerShell with administrative privileges. Or you can check if you might have created a profile for PowerShell before then open it by running the following command.
    Test-path $profile
  2. If the command returns ‘True’, you already have a profile. You can access it by running the command below.
    notepad $profile
  3. If the command shows ‘False’, then you do not have a PowerShell profile. You can create it with the command below.
    New-item –type file –force $profile
  4. After opening this command, a new notepad will open. Here you can add new profiles to it. Also, change the execution policy in PowerShell. This will allow the file to be loaded when you open PowerShell.
  5. Run the command below and close PowerShell.
    Set-ExecutionPolicy RemoteSigned
  6. By default, the profile file will be created in the following location.
  7. The profile is works for current user only. If you have another user working on the same system, they will not have access to it and running the test command when signed in with the other user account will show as ‘False’.

If you have done the above steps, then you have successfully created a PowerShell profile on Windows 10. To access the created profile you need to use the below command.

You can do great things using PowerShell. Microsoft has given some detailed PowerShell profiles on its website.

PowerShell, like many command lines, lets you create a profile. The profile allows you to customize the environment that you get when you open it. Out of the box, PowerShell does not have a profile file on Windows 10. This is because the file is basically a script that runs when you open PowerShell and by default, scripts are disabled as a security measure. Here’s how you can create a PowerShell profile on Windows 10.

You will need admin rights to create a PowerShell profile.

Create PowerShell profile

Open PowerShell with admin rights. If you want, you can check if you might have created a profile for PowerShell before and simply forgotten about it. To check, run the following command;

If the command returns ‘True’, you already have a profile. You can access it by running the command below;

How to create a powershell profile

If the command returns ‘False’, then you do not have a PowerShell profile. You can create it with the command below.

When you run this command a new Notepad file will open. This is your PowerShell profile file and you can start editing it. You do have one more thing to do; change the execution policy in PowerShell. This will allow the file to be loaded when you open PowerShell.

How to create a powershell profile

Run the command below and close PowerShell

By default, the profile file will be created in the following location.

This profile is for the current user only. If you have another user configured on the same system, they will not have access to it and running the test command when signed in with the other user account will return a ‘False’.

Editing PowerShell profile

Any time you want to edit the profile file, you can use the following command to open it. As for what edits to make, those are entirely up to you. The profile isn’t needed to use PowerShell but setting one up might make daily tasks easier for you, if you use PowerShell a lot.

There are a few useful things you can add to the profile, e.g., display the current time and date when you open PowerShell, or give the PowerShell window a specific title, among other things. You can also add scripts to the profile. This will allow you to run them more easily when you’re working in PowerShell. The profile can also be used to modify the look of the PowerShell window extensively.

In the event that you enter something incorrect in the file, it simply won’t load. It is unlikely that a profile file will cause problems in PowerShell.

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

How to create a powershell profile

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!

Here I will show you exactly how to set up a PowerShell profile;

Creating your PowerShell Profile

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

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.

At the Windows PowerShell prompt, type the following command, and then press ENTER:

Personalising your Profile

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

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:

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

How to create a powershell profile

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!

How to create a powershell profile

How to create a powershell profile

The biggest IT trends in 2020

In this post we explore some of the biggest IT trends in 2020 and how Covid-19 has impacted on the modern workplace dramatically in the last 10 months.

How to create a powershell profile

Reimagining the workplace post Covid-19

What will the modern workplace post Covid-19 look like? One things for sure, we won’t be returning to ‘normal’ business operations any time soon. Learn more >

How to create a powershell profile

Reimagine everyday with Cloud Business

Welcome to our new website and our new brand. If you’re familiar with Cloud Business you will have noticed that we’ve had a radical brand refresh to better reflect who we are in

In the previous tutorial, we showed you how to change your window size and colors of background and text. If you are not familiar with the previous tutorial, please look into it. You may have noticed that when you closed your window, the changes were not saved. This tutorial will show you how to have these settings saved and make them the default settings every time you open PowerShell.

If you have not already done so, open Windows PowerShell. If you are a frequent user, it should most likely show up on the most used programs list. Go ahead and open Windows PowerShell Command Shell.

Also, you will need notepad open as well.

How to determine if you have a profile? Simple, in the command shell, type in:

Your profile will then be listed. It would normally resemble something like this:

If you so happen to have an existing profile, open it up in Notepad; otherwise create a blank text file using Notepad in the pointed located by the profile command. The profile, in reality, is simply a PSH script that gets executed whenever a shell is launched.

If you are going to create a profile file, you will need a Windows PowerShell folder in your Documents folder if it doesn’t already exist of course.

Step two.

Save this file and open a new PowerShell window. You will then be confronted with an error. No worries, this is simply Microsoft’s way of protecting you. PowerShell has the ability to allow its users to make malicious scripts and deliver them for others to run. Most of the scripts are of course disabled with the proper settings placed in PowerShell. PSH will not let you run any script, not even your profile, unless it has been signed using a trusted certificate issued by a Certificate Authority or a self-generated certificate using the Microsoft .NET Framework Software Development Kit (SDK).

For now, to get rid of the error to run your own script and see how it looks like, simply run the following command:

And by default the execution policy is Restricted. In other words, no scripts can run and only interactive commands are allowed. Again, it is for your own protection as well as others.

Once this has taken place, close the current PSH window and open a new one, you will see the title, color and window size change at once.

Remarks last but not least…

Remember, the RemoteSigned command only works for locally stored scripts. By saving these changes to your profile, it will automatically save your settings as default and load PSH with these settings. Saves you from having to change them all the time! Join us next time for more PSH tutorials! Till then…

@lowellheddings
January 20, 2010, 12:00pm EDT

For frequent PowerShell users, the standard settings might not be ideal. We can change the settings of our PowerShell window to how we like it by modifying the profile.

The first thing we need to check is whether or not a profile already exists. We can do this by opening a PowerShell window and typing:

How to create a powershell profile

Since our query returned “False” we are going to have to create a new profile. We’ll type the following in our PowerShell prompt:

How to create a powershell profile

You should get the following pop-up, and just click yes to proceed.

How to create a powershell profile

The profile script will now be created. It will be in the location following the “Directory:” output. When we browse to that location, the profile script will be there waiting for us to modify. Open the file named “Microsoft.Powershell_profile.ps1

How to create a powershell profile

The profile will be empty, so we can fill it with any commands that we want to run. You can set the starting prompt location to the root directory by adding:

We can change the title of the PowerShell window to SysadminGeek by adding:

We can also change the window size and scrollback with the following:

$Shell = $Host.UI.RawUI
$size = $Shell.WindowSize
$size.width=70
$size.height=25
$Shell.WindowSize = $size
$size = $Shell.BufferSize
$size.width=70
$size.height=5000
$Shell.BufferSize = $size

The background and text coloring can also be changed with these entries:

$shell.BackgroundColor = “Gray”
$shell.ForegroundColor = “Black”

You can put any scripts or aliases that you commonly use as well. We’ve included our alias for Notepad.exe

new-item alias:np -value C:WindowsSystem32notepad.exe

The final entry is included to clear everything from the PowerShell window, giving you a clean workspace:

All together, it looks like this:

How to create a powershell profile

After you’ve saved your profile, the next time you open PowerShell you will see all the changes made to your profile.

How to create a powershell profile

It’s pretty easy to customize your profile, and the more you use PowerShell, the more you will find yourself returning to add in new aliases and scripts that you need to run at startup.

by michael.simmons | May 27, 2010 | Uncategorized

Whether you’re new to PowerShell or not, when you find yourself ready to start customizing PowerShell and making it do a few tricks, you’re going to want to setup your profile.

Don’t worry, I will show you how to create your profile, and even show you how to add a few cool things to your profile. You are about to develop the skills to tame that PowerShell profile!

Advertisement

My Profile Was So Confusing When I Started

I knew it was there, but I couldn’t even find where it was supposed to be. There were some things that I thought I could do with my profile, like:

  • Define a function.
  • Load a snapin.
  • Change the background color of the shell.

But it always seemed like a lower priority than learning some other, more “immediate need” PowerShell skills.

Once I decided it was time to jump in and customize my profile, I was shocked at the first discovery.

Your PowerShell Profile is not Created by Default

The path to it is there, but you have to create it yourself. Learning how to create a great PowerShell profile was NOT the hardest thing about learning PowerShell. But it WAS one of the most frustrating because I knew that it was cool stuff that was just out of my reach.

Here’s how you setup your profile. First, you can see that the $profile variable is populated, and points to a file.

PS C:\> $profile
C:\Users\Michael\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

But that file doesn’t actually exist

See? Crazy. So I need to create the profile. I use new-item to do this, and use the $profile variable to define where I’m creating the item

The “-force” parameter really helps. It means “create this item and if any part of the directory tree doesn’t already exist then create that too.” Very nice. I forgot to add the “-type” for the new-item command, so it prompted me for it. I left that in there so you can see what it looks like if you forget. To create it properly, I should have done “new-item $profile -force -type file”. Either way, we get the result:

Mode LastWriteTime Length Name
—- ————- —— —-
-a— 5/27/2010 10:32 PM 0 Microsoft.PowerShell_profile.ps1

The profile has been created! Since it’s a .ps1 file, we can invoke our default .ps1 editor by using Invoke-Item.

“ii” is an alias for invoke-item. It means “open the file with the default program”. Invoke-item on a .txt file or .ps1 and it opens in your default editor – notepad by default, but you should switch to a better text editor, and the sooner the better.

Invoke-item on an .mp3 will play the .mp3 with the default .mp3 player – Windows Media Player, Winamp, or iTunes, usually. I use that as a piece of a timer and stopwatch.

That that you have finally created a profile, and opened it, you can write something simple in it so you know it’s there.

“Hello World. This is my profile”

Start a new PowerShell session and you’ll see it working. You’re not done yet, though.

Do the same thing in both the Shell and the ISE. Two separate shells, two separate profiles. You have to create your profile for each shell that you use, if it doesn’t already exist.

Once you’ve created your profiles, you’re ready to add some cool stuff in there. Here are some customizations you can play with:

What awesome things do you have your profile doing? Let me know, and if you’ve blogged about it I’ll link over to you.

In this post we will look at some of the mechanics behind creating a profile file which will customise the start-up behaviour of the Integrated Scripting Environment (ISE). This will also serve as a precursor to additional posts on tweaking ISE for Exchange purposes. The catalyst for writing this was two fold. Firstly, an administrator was having issues trying to get ISE to process a profile script, and secondly they were not connecting to Exchange PowerShell in a supported manner.

We will start off by looking at the default configuration, and seeing what happens with ISE when we use the location a lot of folks place their profile content in. This is the $PROFILE location in Windows PowerShell.

Starting Configuration

The below screenshots are taken from a Windows 2008 R2 SP1 server. Initially there are no PowerShell profiles on the system. We are piping the $PROFILE variable to Format-List with the –FORCE parameter to show all of the profile paths. Running $PROFILE on its own does not show all this detail.

How to create a powershell profile

Let’s go ahead and create a profile script in the $PROFILE location. This is what the administrator I was working with was doing.

Creating PowerShell Profile

In the $PROFILE location, let’s create a script file that contains the below code. This will write a phrase to the screen at the start and end of the profile script. Additionally it creates a variable called $Zorg that we can retrieve for testing purposes.

Write-Host “Starting profile” –ForegroundColor Magenta

Write-Host “Completed Profile” –ForegroundColor Blue

The file is created and saved:

How to create a powershell profile

We can confirm the file is present in the directory.

How to create a powershell profile

Sweet! let’s test this badboy in Windows PowerShell.

Testing In Windows PowerShell

Note that the script execution policy must be set to allow scripts to run. Installing Exchange will set this automatically. Else Set-ExecutionPolicy can be executed from an elevated Windows PowerShell session. When we then launch Windows PowerShell, the profile file is processed.

How to create a powershell profile

Yay!! All is good and now we can just load it up in ISE? Right?

Well not so much…..

Firing Up ISE

Launching ISE on the server produces les than spectacular results. In fact there is nothing.

How to create a powershell profile

Did we not just see that the profile script was successfully launched? Where is the disconnect?

Let’s go back and double check our file information.

Checking PowerShell Paths

Take a look at $Profile inside ISE. Does it not have a $Profile variable??

But is exactly the same?

How to create a powershell profile

This is the ISE $Profile

C:Usersadministrator.contosoDocumentsWindowsPowerShellMicrosoft.PowerShell ISE _profile.ps1

Compare this to the PowerShell $Profile

C:Usersadministrator.contosoDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1

The files are in the same directory. But note that the there is a difference between the file names.

Let’s remove the windows PowerShell profile and put our settings into the ISE profile.

How to create a powershell profile

Firing Up ISE – Take 2

This time we have what we are looking for!

In the ISE results pane we can see the starting and completed text. In addition we can also see that there is a variable called $Zorg and the contents are retuned as expected.

How to create a powershell profile

Digging In

Looking at the article How to Use Profiles in Windows PowerShell ISE we can see that it references the profile types that are supported.

Profile Type

ISE Path

The designers or PowerShell were smart! They gave us the capability to have different profile files. Some apply universally across the server to all of the PowerShell hosts that are executed on that particular server. Some are user and host specific. In other words we need to ensure that we are using the correct PowerShell profile script for the correct purpose.

In this post’s initial example, we had a disconnect between the $PROFILE variable which is the “current user, current host”. There is a “current user, current host” profile script for BOTH ISE and also for Windows PowerShell.

A similar concept existed for VBScript as it could be hosted in Windows Scripting Host (WSH) or Internet Explorer. Though it kind of lacked profile files, and a lot of other things as well compared to PowerShell…..

If we look at examples of where the Windows PowerShell and ISE profile scripts are located, we can see that some of the profiles align and some do not. Files which align are the AllHosts ones as they apply to all of the environments that can host PowerShell on a specific server.

Windows PowerShell Profile Locations

$profile.CurrentUserCurrentHost ($Profile )

$profile.AllUsersCurrentHost

C:WindowsSystem32WindowsPowerShellv1.0Microsoft.PowerShell_profile.ps1

$profile.CurrentUserAllHosts

$profile.AllUsersAllHosts

C:WindowsSystem32WindowsPowerShellv1.0profile.ps1

ISE PowerShell Profile Locations

$profile.CurrentUserCurrentHost ($Profile )

$profile.AllUsersCurrentHost

C:WindowsSystem32WindowsPowerShellv1.0Microsoft.PowerShellISE_profile.ps1

$profile.CurrentUserAllHosts

$profile.AllUsersAllHosts

C:WindowsSystem32WindowsPowerShellv1.0profile.ps1

Conclusion

The key takeaway is that you need to use the right profile for the right purpose. Should you only want the profile script to apply to Windows PowerShell then use either one of the CurrentHost profiles. However if the profile script is to only apply when using ISE then leverage one of the PowerShellISE profiles.

In the below example the same profile settings should apply to all instances on the server. Thus the AllUsersAllHosts profile was used.

In the below screenshot you can see that the Exchange Management Shell, Windows PowerShell and ISE have all processed the AllUsersAllHosts profile script.

How to create a powershell profile

In the next post we will see how to connect to Exchange 2010 and 2013 Remote PowerShell using ISE.

I want to create an alias of a cmdlet that doesn’t expire after I close the current session of Powershell, let’s say I have this alias :

This perfectly creates the Goto alias, but I want to use it even after I close the current session, how can I achieve that.

Note:

The PowerShell Help system suggests that I can export the aliases I create, and import them next time I open a new session, actually that’s not really what I’m looking, for, is there a direct clear way to keep having a alias after I create it through different sessions

How to create a powershell profile

How to create a powershell profile

8 Answers 8

UPDATED – Jan 2017

It’s possible to store in a profile.ps1 file any powershell code to be executed each time powershell starts. There are at least 6 different paths where to store the code depending on which user have to execute it. We can consider only 2 of them: the “all users” and the “only your user” paths.

So to answer your question you only have to create a profile.ps1 file containing the code you want to be executed, that is

and save it in the proper path:

  • “$Home\Documents” (C:\Users\yourname\Documents): only your user will execute the code.
  • $PsHome (C:\Windows\System32\WindowsPowerShell\v1.0): every user will execute the code.

To apply the changes close all open powershell instances and restart them.

TIPS

If both paths contain a profile.ps1 file the all user one is executed first, then the users specific.

Always put the code in the user specific profile if there is no need to extend its execution to each user. You don’t need administrator rights to add the file to your user space (otherwise you do) and you don’t pollute other users’ space.

REMEMBER! The $PsHome path is different between 32bit and 64bit instances of powershell, so you have to consider both environment if you want always to execute the profile code. Usually the paths are C:\Windows\System32\WindowsPowerShell\v1.0 for the 64bit environment and C:\Windows\SysWow64\WindowsPowerShell\v1.0 for the 32bit one.