Solved

How Do I implement multiple powershell profiles

Posted on 2014-09-11
4
156 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
  • 3
4 Comments
 
LVL 70

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 70

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 70

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now