New PSDrive inside the module does not work
I have a module that automatically loads where I put my day to day functions and variables.
In the hope that PSDrive is always there, when I start a PowerShell session, at the very beginning of MyPSModule I call:
$script_directory = 'D:\scripts\'
New-PSDrive -Name Scripts -root $script_directory -PSProvider FileSystem
However, after opening a new PowerShell environment and loading my module: Get-PSDrive
does not list my drive. (I'm sure my module is loaded, I called some of its functions, I even re-imported with -Force
, and Import-Module -Verbose
didn't find any errors)
I need to manually call: New-PSDrive -Name Scripts -root 'D:\scripts\' -PSProvider FileSystem
. Only then will Get-PSDrive
my disk be listed.
What's wrong? What should I do when the PSDrive is created when I load my module?
source to share
Use parameter Scope
with valueglobal
-Scope
Specifies the scope of the disk. Valid values are "Global", "Local", or "Script", or a number relative to the current region (0 through the number of regions, where 0 is the current region and 1 is its parent). Local by default. For more information see About_Scopes ( http://go.microsoft.com/fwlink/?LinkID=113260 ).
$script_directory = 'D:\scripts\'
New-PSDrive -Name Scripts -root $script_directory -PSProvider FileSystem -Scope global
Explanation of the Scop parameter:
Global: . The scope in effect when Windows PowerShell starts. Variables and functions that are present when Windows PowerShell launches were created globally. This includes auto variables and preference variables. This also includes variables, aliases, and functions that go into your Windows PowerShell profiles.
Local: The current area. The local scope can be the global scope or any other scope.
Script: The area created while the script is running. Only commands in script within script scope. To commands in a script, the script scope is the local scope.
Private: Items in a closed area are not visible outside of the current volume. You can use a private area to create a private version of an item with the same name in a different area.
source to share