简体   繁体   中英

Creating files at PSModulePath in batch

I am currently trying to write a batch program that installs a module named SetConsolePath.psm1 at the correct location. I am a beginner with Batch and I have absolutely no powershell experience.

Through the internet, I have learned how to display PSModulePath with powershell -command "echo $env:PSModulePath .

How can I, via .bat file, move SetConsolePath.psm1 from the desktop to the location displayed by powershell -command "echo $env:PSModulePath ?

Thank you in advance, and I apologize for my lack of experience.

Before I answer, I must out that you do not want to copy PowerShell module files directly to the path pointed by PsModulePath . You really want to create a folder inside PSModulePath and copy the files there instead.

The prefix env in a Powershell variable indicates an environment variable . $env:PSModulePath is actually referring to the PSMODULEPATH environment variable. On the command line, and in batch files, environment variables can be displayed by placing the name between percent symbols. (In fact, you could have displayed this value by typing echo %PSMODULEPATH% instead.)

To reference the desktop folder, have a look at this answer , which shows you how to use another environment variable, USERPROFILE .

Therefore, to copy the file from the desktop directory to the path specified in PSModulePath , you would do this:

COPY "%USERPROFILE%\Desktop\SetConsolePath.psm1" "%PSMODULEPATH%"

And, as I warned earlier, you really should copy the file to a folder underneath PsModulePath . So what you really want is:

IF NOT EXIST "%PSMODULEPATH%\MyNewFolder" MKDIR "%PSMODULEPATH%\MyNewFolder"
COPY "%USERPROFILE%\Desktop\SetConsolePath.psm1" "%PSMODULEPATH%\MyNewFolder" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM