简体   繁体   中英

Running a psm1 PowerShell script in Command Prompt (cmd.exe)?

I know nothing of PowerShell, but I wanted to install this: https://www.powershellgallery.com/packages/lolcat/

So, I start PowerShell as administrator, and:

PS C:\WINDOWS\system32> Install-Module -Name lolcat                                                                                                                                                                                             NuGet provider is required to continue
PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet
 provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
'C:\Users\Me\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running
'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import
 the NuGet provider now?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): y
PS C:\WINDOWS\system32> lolcat

Usage: lolcat [OPTION]... [FILE1[, FILE2[, ...]]]
...

Nice, it works. So first I find where is the newly installed script:

PS C:\WINDOWS\system32> (Get-Module lolcat).Path
C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1

Ok, so now I want to try calling this from cmd.exe:

C:\Users>PowerShell.exe -File "C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1"
Processing -File 'C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1' failed because the file does not have a '.ps1' extension. Specify a valid Windows PowerShell script file name, and then try again.
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

Nope, does not work.

Is it possible to call this PowerShell script from cmd.exe - and if so, how?

The error is due to the fact that the -File parameter from the powershell.exe excepts a .ps1 file.

If you want to run C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1 from cmd, make a .ps1 script where you can write something like

Import-Module lolcat

# now you have all the functions from the lolcat module loaded into this PowerShell session

# do stuff

And then call this script from cmd.

The difference between .ps1 and .psm1 is explained here .

Eh, turned out to be less complicated then I thought - since it is a "module", just use the module name:

C:\Users>PowerShell.exe lolcat

Usage: lolcat [OPTION]... [FILE1[, FILE2[, ...]]]

...

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