File Explorer won't display PowerShell Modules directory otherwise visible in $env:psmodulepath - powershell

I'm in the process of learning how to create powershell modules through this youtube tutorial https://www.youtube.com/watch?v=yKstEJKdc4o&t=3661s - the video itself is not important.
I learned from learn.microsoft.com that powershell will have the following module directories by default:
$PSHome\Modules (%Windir%\System32\WindowsPowerShell\v1.0\Modules)
$Home\Documents\WindowsPowerShell\Modules (%UserProfile%\Documents\WindowsPowerShell\Modules)
$Env:ProgramFiles\WindowsPowerShell\Modules (%ProgramFiles%\WindowsPowerShell\Modules)
however there is no C:\users\john\Documents\WindowsPowerShell\Modules folder visible in File Explorer, while it's clearly visible in $env:psmodulepath when I run this from PowerShell. I'm able to navigate all the way to C:\Users\john\Documents\WindowsPowerShell but there is no Modules directory. I did enable hidden folders display and did also check Documents folders for OneDrive, Public, Administrator users. This is not a work/school computer, this is my own PC.
Can someone please help me understand why this Modules folder is visible in $env:psmodulepath but not in File Explorer?

Just because a location exists in $env:psmodulepath, doesn't mean it actually has to exists in the file system.
You can add any random location to $env:psmodulepath without it actually existing. PowerShell will just skip any locations that don't exist.
With a fresh Windows account, there isn't even a WindowsPowerShell directory under $env:userprofile\documents, to use a user profile or modules you have to create those directories.
It is fairly easy for a user to create a new directory, but a little bit more tricky to add a new entry to $env:psmodulepath. I guess that is why they already put it in there, even though it may never be used.

Related

How to prevent Powershell Modules being installed into OneDrive

My $env:PSModulePath has ended up containing OneDrive:
C:\Users\jack\OneDrive\Documents\PowerShell\Modules;C:\Program Files\PowerShell\Modules;c:\program files\powershell\7\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
But I don't want all my Modules being synced to the cloud.
According to the docs:
The user-specific CurrentUser location on Windows is the PowerShell\Modules folder located in the Documents location in your user profile ... Microsoft OneDrive can also change the location of your Documents folder.
So that explains how the Modules ended up there. But how do I fix it?
I can modify $env:PSModulePath but this will only allow pwsh to find more modules I think. It won't change the default path for installing new modules for CurrentUser.
P.S. I face the same problem with the Update-Help when it saves help locally... OneDrive :-(
Perhaps the information in this thread on Reddit, could be of help.
Here's a snip from one of the answers, talking about excluding certain folders:
You can accomplish this by opening the OneDrive settings on your PC.
Go to OneDrive settings
Click the Account tab
Click "Choose Folders" and then cherry pick the folders you want to
exclude.

Change Directory to Folder Containing PowerShell Script - Regardless of Where That Folder Is Located

I have a script that I've created to prep our customer's servers for a software install. Part of this requires the script to be run as administrator, so just instructing people to click "Run With Powershell" doesn't get the job done. The script is in a folder with a number of .ini files that the script needs to copy to different server locations. If I just right-click the Powershell script and select "Run With Powershell," it is able to find the files and copy them without issue. Unfortunately, if I open the script in ISE, it opens with a default directory of C:\users\user, and I can't seem to copy those .ini files without first running a change directory command to get us to the folder that the script and the .ini files are in. But I'd like our installation techs to be able to run this without worrying about the exact location they initially drop these folders. I'd also like them to not have to worry about changing the directory manually in PowerShell. Some of our customers have multiple drives, and it might make sense to put this stuff on something other than the C drive, so it's hard to tell where this folder might end up. But I'm not sure of a command that will get me to the directory of the *.ps1 file, without knowing where that file is beforehand... Anyone have a suggestion?
You can use $PSScriptRoot that will have the location of the directory where the script is located.
This is referenced in the following post:
How can I get the file system location of a PowerShell script?

Where can I put Powershell modules that will be accessed by multiple users?

I used the variable $Env:PSModulePath , It provided two paths.
> C:\Users\My User\Documents\WindowsPowerShell\Modules
> C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
Seems like My User will be accessible only for me. But my module will be used by multiple person and also when uninstalling my application [I am installing my module via msi], even if some other user uninstalls it should be removed.
Can I use "C:\Windows\system32\WindowsPowerShell\v1.0\Modules\" for my application related module? Or is there any other better place ?
Can i use "C:\Windows\system32\WindowsPowerShell\v1.0\Modules\" for my
application related module?
No. That place is not for you as noted in the section "Rules for Installing Modules" of the module installation guidelines.
You can decide yourself where you want to put the modules that are shared for multiple users.
Some options include:
Create new folder in Program Files, and add that path to PSModulePath.
Use a network share (though this means you need to change your execution policy to allow this), as #Rubanov said.
Use the All Users folder for example.
You can also put your psm1 files on a shared drive. Then import the module with
Import-Module -Name "\\SERVERNAME\Use-MyModule.psm1"
You can find more info about adding a path to PSModulePath here.

How do I edit the system definition files for quicklisp

I'm trying to install FUF/SURGE on my windows computer, and got stuck on a step.
Instruct quicklisp where to find the system definition files to load
fuf and dependent modules. Create a file in this folder:
~/.config/common-lisp/source-registry.conf.d with this content:
(:source-registry
(:tree (:home "fuf"))
:inherit-configuration)
I was wondering how do I get to the file listed above and how do i edit it? I have emacs installed with quicklisp/slime.
OK so you need to make these subfolders in your home directory. I'm guessing you installed under windows as I saw your comment on my video! Remember that part of the install was created a home folder and setting it's path in your environment variables.
~ is the symbol for the home directory so you need to create directories for this path ~/.config/common-lisp/source-registry.conf.d\ which for my setup would be c:\home.config\common-lisp\source-registry.conf.d\
Inside that create directory creare a fuf.conf file and paste the lines from the instructions.
I haven’t got a windows machine to hand but hopefully that's enough to get you going!

Is there a good reason why PowerShell uses the Documents folder as a default location for scripts?

Is there a good technical or other reason why the MS hard-coded the Documents folder as the default location for WindowsPowerShell? MS has been criticized for too much configuration over convention in the past (WCF?), but a case can be made for more configuration in PowerShell. I, and I presume most developers, like to keep their development work centralized in a separate folder or volume away from personal and system files.
For instance, if you install PoshGit, it will install itself in C:\Users\Your Name\WindowsPowerShell\Modules. I don't want it to install itself there but in my own development partition d:\Dev\PowerShellScripts. There's no environment variable that controls this location.
Is there a reason for this or I just don't get it?
Can you explain yourself a bit more.
According to my understanding PowerShell.exe interpreter base directory is the one defined by $env:HOMEDRIVE, $env:HOMEPATH, that can be change using the user profile.
As shown in the screen shots here under :
Edited :
Ok, the screenshot comes from the user property in Active Directory MMC, you've got a simplest one in your windows seven user properties. But this has nothing to do with your problem.
Your problem is around the module installation. The think that you have to know is that Modules can be installed quite everywhere (even on a shared directory with some tricks). By default the environnement variable $env:PSModulePath points to the paths where Get-Module -ListAvailable look for them. So you can add d:\Dev\PowerShellScripts\Modules in this path and then copy the subfolder of C:\Users\Your Name\WindowsPowerShell\Modules created by PoshGit inside your Modules directory and it should work. Modules as opposite to Snapins don't need to be registered.
Now the reason why PoshGit choose to put module in user profile, raser than let you choose the place is PoshGit installer problem.
For more explanations read about Modules and about_environment_variables.