Solved

How Do I implement multiple powershell profiles

Posted on 2014-09-11
4
160 Views
Last Modified: 2014-09-14
I want to be able to use a powershell profile while I am testing and developing scripts.  
When I am satisfied with the scripts I move them to another directory.  
I want the same machine to use only those scripts in that 2nd directory when it runs throughout the day.

I assume that using different profiles is the way to go, but I don't know how to implement it.  If there is a better way of doing it I'm open to that, using another machine is not acceptable.

Thanks
0
Comment
Question by:c7c4c7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 40320589
You could set-up short-cuts (aliases, or any other method you like) to open PowerShell with specific profiles by dot-sourcing scripts:
powershell.exe -command ". C:\DevProfile.ps1" -NoProfile

Open in new window

That'll load your DevProfile script, but won't load the default profile scripts.

Chris
0
 

Author Comment

by:c7c4c7
ID: 40321279
So if I start script "a" with . \ and a specific path I get exactly what I need but then if script "a" calls "script b" would I then have to place the call in "a" with a specific path to "b"?
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 40321487
It's this you want:
. <PathToScript>

Open in new window

If the script were in the current directory it would be:
. .\Script.ps1

Open in new window

An absolute path may be specified in a similar manner:
. c:\somewhere\scripts\script.ps1

Open in new window

The existing profile scripts execute in a similar manner (dot-sourced, and therefore execute in global context), so if you want substitution and choice of profiles then this is one way to do it.

There are a myriad of different ways to end up running that particular profile script. From menu-driven options, to short-cuts, to starting PowerShell using a function, etc, etc. The important thing is to find out whether or not this offers you the separation you want.

Chris
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 40321491
Sorry, I failed to address the A and B thing. Ultimately it depends entirely on what you want to happen (and what script B does).

If script A is dot-sourced, and script B is not then script B would execute in the same manner as if you called it from the console (c:\scripts\scriptb.ps1). Variables and functions declared within script B would have Script scope (and be inaccessible outside). Conversely, variables and functions in script A would have Global scope.

Dot-sourcing is a useful method for executing scripts as if you'd typed it directly into the console. You get something like the same effect when you use Import-Module, although it's use is a bit more specialised.

Dot-sourcing is used (in the background) to execute the default profile scripts exposed in the $PROFILE reserved variable ($PROFILE | Select-Object *).

Chris
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question