Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PS Profiles 1

Status
Not open for further replies.

markdmac

MIS
Dec 20, 2003
12,340
0
0
US
I am curious if others are also using the PowerShell profiles to create aliases or configure their PS environment?

Are people even aware of this capability? For example, I like the vbscript Now function and would prefer to use Now instead of Get-Date and I don't want to set up an alias each time I launch PowerShell. I would also like to have instant access to just the date and time separately. So, I have edited my Profile to set this up for me at launch.

To edit your profile go to My Documents\PowerShell and edit the file Microsoft.PowerShell_profile.ps1 with notepad. Here is the contents of my file:

Code:
Set-Alias Now Get-Date
$Date = Get-Date -Format M/d/yyyy
$Time = Get-Date -Format hh:mm:ss

If the file does not already exist you will need to create it.

First determine if the Profile is created with
Code:
Get-Content $Profile

If you get an error then create the profile file and add data to it all in one shot.
Code:
Add-Content $Profile -value "Set-Alias Now Get-Date"
Add-Content $Profile -value "$Date = Get-Date -Format M/d/yyyy"
Add-Content $Profile -value "$Time = Get-Date -Format hh:mm:ss"

Note that in order for profiles to work you have to have configured PowerShell security to execute scripts. To do that you need to execute the command:
Set-ExecutionPolicy -executionPolicy Unrestricted

Note that the above will allow all scripts to be run, if you use signed scripts then don't use the Unrestricted switch and instead use AllSigned. I suggest everyone look at the options with Help Set-ExecutionPolicy.

I want to point out also that it is possible from within a PS Session to export/import your Aliases. Use caution when doing this. By default ALL aliases will be exported or imported. You will then get a bunch of errors when the default aliases try to remap themselves at launch/import. It is however possible to export named aliases instead of all aliases. This is the preferred method to avoid unnecessary errors.



I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
I've seen people refer to profiles, but haven't seen to many examples on setting one up. I also read of someone who created, for example, a function profile where he kept his functions and was able to import it into his regular profile. Have you tried this? What do you think of that approach of having a profile that perhaps contains functions only and another that contains aliases and so on and then import them to a main profile?

dm4ever
--------------------------------------------------------------------------------
My approach to things: K.I.S.S - Keep It Simple Stupid
 
I don't care much for that idea only because the whole reason to use a profile is to make it easy for you to standardize shortcuts. In an enterprise I would want to be able to standardize all of my servers aliases and functions so my scripts can work on any system without further action.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top