New-PSDrive inside a module doesn’t work


I have a module, which is auto-loaded, where I put my everyday functions and variables.

In the hope that a PSDrive is always here when I start a PowerShell session, at the very beginning of MyPSModule I call:

However, after I open a new PowerShell environment, and that my module is loaded: Get-PSDrive doesn’t 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 doesn’t show any errors )

I have to manually call: New-PSDrive -Name Scripts -root 'D:\scripts\' -PSProvider FileSystem.
Only in such a case will Get-PSDrive list my drive.

What is wrong ? What should I do so as the PSDrive is created at the loading of my module ?


Use the Parameter Scope with value global


Specifies a scope for the drive. Valid values are “Global”, “Local”,
or “Script”, or a number relative to the current scope (0 through the
number of scopes, where 0 is the current scope and 1 is its parent).
“Local” is the default. For more information, see about_Scopes

Explanation of Scop parameter:

Global: The scope that is in effect when Windows PowerShell starts.
Variables and functions that are present when Windows PowerShell
starts have been created in the global scope. This includes automatic
variables and preference variables. This also includes the variables,
aliases, and functions that are in your Windows PowerShell profiles.

Local: The current scope. The local scope can be the global scope
or any other scope.

Script: The scope that is created while a script file runs. Only the
commands in the script run in the script scope. To the commands in a
script, the script scope is the local scope.

Private: Items in private scope cannot be seen outside of the current
scope. You can use private scope to create a private version of an
item with the same name in another scope.


New-PSDrive inside a module doesn’t work by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply