Great post courtesy of Fermin Sanchez
Creating PowerShell script library
Over time I’ve created some PowerShell scripts that I want to reuse. And of course I would like to have them at my fingertips as soon as I start a PowerShell session.
The solution to this is to create a share on the network with all my scripts and using the New-PSDrive command to map it. And since I don’t want to do that every time I open a PowerShell session, I’m going to put this in my profile.
First, open a PowerShell session and check out where the profile file is and if it already exists:
The result of Test-Path shows that the profile does not exist, yet. Regardless, the next command is used to either open an existing profile file or creating a new one:
As you can see I’ve added two commands to my profile. The first will create a PSDrive “pslib” which really just works like a network drive letter; you’ll have to adjust the path to your network share accordingly. The second command, “Set-Location pslib:” makes sure that the command prompt will start at my “psdrive:” location instead for instance “c:”.
After saving the profile and opening a new PowerShell session, this should happen:
OK, so that’s now all good for this machine. What about the rest of the machines in the network? Easy, use Group Policy Preferences!
As you have noticed, the profile file is located in the user’s profile path, so we need to generalize “c:\users\administrator….” to “%userprofile%\…”.
First, create a new Group Policy Object or open an existing that you want to use (for example a group policy that will be applied to all your servers). Navigate to User Configuration > Preferences > Windows Settings > Files and create a new item:
This is the source where you keep a copy of the profile file created above. I’m using my domain’s netlogon share. Any other share will work, too.
This should be %userprofile%\Documents\WindowsPowerShell
And that’s it! Save the configuration item with OK, wait for replication to take its course or force it, and all machines where this GPO applies should now automatically map your “pslib” drive.