I am creating a suite of custom cmdlets and providers in C#. I also have a PowerShell SnapIn that takes care of registering the cmdlets and providers. I have exported a console session so that I can start PowerShell with the -PSConsoleFile parameter to automatically load my snap in.
I also want to mount a drive whenever I run PS with the snap in. Effectively, I want the following to run at the start of the PS session:
new-psdrive -name [drivename] -psprovider FileSystem -root [path to a folder on local file system]
I have tried putting the above command in a .ps1 file and starting PS with -command and the path to the .ps1 file, also specifying the -NoExit flag. The script does run, but the drive is not mapped in the subsequent session.
Is there a simple way to create a new psdrive in the snap in? I also looked into deriving from the FileSystemProvider but it is sealed. I looked into running the NewPSDriveCommand programmatically but that does not appear to be supported.
Am I missing something simple here?
Thanks!
Edit: I forgot to mention that I don't want to use the the profile file to accomplish this if possible. I want to distribute this snap in to others and I would rather they didn't have to edit their profiles to get everything to work.
You might try adding the statements to your Powershell global or environment profile. Both are located at %username%\My Documents\WindowsPowerShell. Your global profile is named profile.ps1 and your environment profile is named for each shell (Microsoft.PowerShell_profile.ps1 for the default Powershell environment).
I have a couple of new-psdrive statements in my profile.ps1 file. The downfall to this method is waiting on Powershell to connect all of those PSDrives if they are on a slow server.