Solved

How Do I implement multiple powershell profiles

Posted on 2014-09-11
4
159 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
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.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 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