If you’re writing PowerShell scripts the right way, you’re testing your scripts against a Test, or DEV environment prior to deploying those scripts to your Production environment (You ARE testing your scripts right?). If you’re using source control for your scripts (You ARE using source control for your scripts right?) then you’re likely using a DEV branch for your in progress script and then when all is ready you can Merge that into MAIN or whatever you call your production branch.
NOTE: If you aren’t using source control, branching & merging, and testing, you’ve probably got bigger problems than your PowerShell profile.
If you are able to remotely run your code against your servers or if you’re like me and you’re managing Office 365, you’re running your test scripts and your production scripts on the same workstation.
Some of the common repeatable steps I have to take to differentiate my DEV and PROD instance are to change my directory I’m running my scripts from and since I write a lot of Modules, I need to ensure I’m using the correct Modules.
Out of the box, you’re going to have to manually change your directory and go in and add a temporary module directory depending on where your DEV and PROD modules are stored. I can write a script to do this and that’s what I’ve done, but instead of calling that script manually, why not incorporate it into your profile?
Below is a simple profile I’ve created for my workstation that allows me to determine what environment I’m working in when I run PowerShell. Now, I do nothing different when I open my Windows PowerShell. When it executes it runs the $profile and my script executes prompting me to enter “DEV” or “PROD” based on what I’m working on at the time.
Here’s the script you will copy and paste into your $profile file on your workstation. See below for the instructions.
What is this script doing?
There are three specific tasks this script is doing.
- Add Temporary Module Path (based on Parameter entry)
- Sets Current Directory (based on Parameter entry)
- Changes Prompt text and color (based on Parameter entry
Follow these steps to include the script above into your Windows PowerShell Profile. Note there are pieces you need to modify in the script above that are wrapped in <>’s. You’ll need to change the paths to represent your own locations.
- Open Windows PowerShell
- Verify you have a $profile created. Click for instructions and information about PowerShell Profiles
- Edit your profile by entering the following command: notepad $profile
- Copy the code above (make sure you’ve changed paths to represent your paths) and paste the code into the $profile notepad file
- Close PowerShell and Re-Open
- You should see a prompt similar to the image below
Once you’ve entered the environment, your PowerShell prompt should look like the below image
Verify Your Profile Environment
It’s wise to verify you got the profile paths correct. Test that the $profile applied the variables as expected by running the following commands
The information returned will be a semi-colon separated result including all current module paths. Your newly added path should be included in this
This should return the current directory your PowerShell session is set to right now. This should reflect your environment path you entered.
You should be able to see the prompt is different depending on the input you chose.
PROD = PROD>
DEV = DEV>
If I Entered “DEV” Do I Have to Close PowerShell to get into “PROD”
No, you do not have to close out, though it might not be a bad idea depending on all the stuff you add to the profile. You can switch easily by entering the following command
. $profile PROD
There is a little bit of error handling, but not much in this script so be aware that you will want to augment this script if you want more error handling.
What can I do with this?
The sky is the limit. If you want to have specific modules loaded or snap-ins you know you’ll use, go ahead and add them. Just be warned, if you add too much stuff, it could slow the overall load time down as well.
Check back to my blog, I’ll show you how you can create a desktop shortcut to automatically enter the parameter so you can quickly get into your DEV or PROD PowerShell profile without entering anything.