Powershell not finding PNPUTIL when script launched from shortcut - powershell

I have a Powershell script to install TCP/IP printers on Windows 10 that uses PNPUTIL to load drivers. When the script is run from a Powershell window, everything works great.
When I launch the script from a shortcut using the format
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -file MyScript.PS1
I get an error 'The term 'pnputil.exe' is not recognized as the name of a cmdlet, function, script file, or operable program' when PNPUTIL is called. The rest of the script runs fine.
Relevant code:
Write-Host `n 'Installing printer driver..'
pnputil.exe /add-driver "\\myServer\HP UPD PCL 5\hpcu180t.inf"
Any ideas as to why this won't work when launched from a shortcut?
EDIT:I tried using
& pnputil.exe /add-driver "\\myServer\HP UPD PCL 5\hpcu180t.inf"
as referenced in
Running CMD command in PowerShell
but I still get the error. I also tried
start-process pnputil.exe /add-driver "\\myServer\HP UPD PCL 5\hpcu180t.inf"
but got a similar error that pnputil.exe could not be found.
Both of these options work from a Powershell prompt, but again, fail when launched from a shortcut.
Thank you in advance.

You're invoking a 32-bit instance of PowerShell on a 64-bit system, and that instance doesn't see pnputil.exe (by filename only).
Instead of:
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -file MyScript.PS1
use:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file MyScript.PS1
Folder C:\Windows\SysWOW64 is where the 32-bit executables live.
Paradoxically, for historical reasons, it is C:\Windows\System32 that houses the 64-bit executables.
If, for some reason, you do need to run a 32-bit instance of PowerShell, you can invoke pnputil.exe by its full path:
It only exists as a 64-bit executable in the 64-bit system folder, which 32-bit processes can access as C:\Windows\SysNative:
C:\Windows\SysNative\pnputil.exe

Related

Start new shell not working when the ps1 has become executable with ps2exe

I am trying to create an executable (.exe) at windows that will perform some actions at first, and then open an interactive shell. In my case the interactive shell I want to be a wsl, but the problem is shown with every type of shells created.
For example:
Supposing that we have the file
test.ps1
Write-Host "Hello from my test program"
Invoke-Expression "powershell"
If I run it with the command: ./test.ps1, the outcome will be to print the message and start a new powershell instance.
If i run the command: Invoke-ps2exe .\test.ps1 test.exe, and then run the test.exe, the outcome will be:
Hello from my test program
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
but I cannot write anything at the window.
Can anyone explain me why this is happening, and how I could overcome this?
Modify your test.ps1 file as follows:
Write-Host "Hello from my test program"
Start-Process -Wait -NoNewWindow powershell.exe
This change ensures that PowerShell's usual interactive host, ConsoleHost, in the newly launched PowerShell session launched from your script doesn't run on top of the simplified host that is built into the .exe files that ps2exe generates, which appears to cause your problems.

Scheduling powershell script to run on server - finding powershell.exe

I'm trying to schedule a powershell script to run on a server. I used $env:PSModulePath and one of the powershell locations is c:\windows\system32\windowsPowerShell\v1.0\Modules. When I look in that location, there is no Powershell.exe there. I wanted to use that for the Application name in the system scheduler. How can I verify that Powershell.exe is there? I also wanted to find which version each one is for all of the powershell versions seen with $env command above to verify which would be the 5.1 version that I tested on my computer with.
env:PSModulePath
c:\windows\system32\windowsPowerShell\v1.0\Modules is the Modules directory
$psHome returns the .exe location

NSIS call powershell cmdlet Update-MpSignature not recognized as the name of a cmdlet powershell

I used NSIS call powershell update windows defender definitions, but when I use nsExec::ExecToStack '"powershell.exe" -c Update-MpSignature', it report an error "Update-MpSignature is not recognized as the name of a cmdlet powershell". I have tried call "powershell.exe" -c Update-MpSignature in cmd, it worked well. I also have tried NSIS offical psexec.nsh ${PowerShellExecFileLog} call Update-MpSignature in ps1 file , other cmdlet like Get-ChildItem、Remove-Item can work very well, but still report an error "Update-MpSignature is not recognized as the name of a cmdlet powershell. Can somebody help me?
This sounds like a 64-bit issue.
On 64-bit systems you could try executing $WINDIR\sysnative\windowspowershell\v1.0\powershell.exe like this header file does.

PowerShell 7.0.2 cannot find DisplaySwitch.exe

Within a Powershell 7.0.2 session with administrator privileges, I've found that I can't list or call C:\Windows\System32\DisplaySwitch.exe.
I can call that application just fine from cmd.exe and powershell.exe (aka "Windows PowerShell"), but oddly enough it can't even be listed by pwsh.exe (the new PowerShell).
Things I've tried so far:
PS> DisplaySwitch
Returns DisplaySwitch: The term 'DisplaySwitch' is not recognized as the name of a cmdlet, function, script file, or operable program.
Or PS> ls C:\Windows\System32\Display*.exe, which returns nothing.
The same two commands above on the classic Windows Powershell (v5.1) work as expected. I.e., it opens the Display Switch tool and lists the DisplaySwitch.exe path, respectively.

"File not found" Can not use Invoke-SCScriptCommand with anything but cmd.exe + args

I have made a plugin for Microsoft System Center Virtual Machine Manager that executes a powershell script on a host machine through a powershell script called by the c# code of the plugin. (Shellception :P)
Since I allways got an error I decided to test it manually in SCVMM by right clicking on the host and entering powershell.exe or powershellfor executable and export-v -name [name] -path [path] -force - copystate -wait.
Now it tells me that there is no such file.
Strangely it works with cmd(.exe) and echo test.
Shouldn't powershell be installed on Windows Server 2012?
Also, if I remotecontroll the host, it works just fine in the console.
What am I missing?
I figured out that you need to provide the full path when using powershell.exe as executable. The issue is that not all hosts have the system variable PATH that includes the path to the powershell.exe executable.
You can run powershell.exe by providing the full path:
%WINDIR%\System32\WindowsPowerShell\v1.0\PowerShell.exe
Or you can run cmd.exe as executable and then to run powershell.exe from this cmd:
executable: cmd.exe
parameters: /c powershell.exe echo 1; return 0;