PowerShell 7.0.2 cannot find DisplaySwitch.exe - powershell

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.

Related

Powershell Script only opens as a .txt file when run

Hi I have a script on a memory stick that I want to be able to run in cmd line before a computer has windows fully installed. (the stage just after you've connected to a network).
Cmd used to run the script.
Start > Run D:\pscript\Intune.ps1
This only opens a .txt file, while researching I've found that the reason this happens is due to security, is there anyway to override this bar changing the default file type out.
Unlike batch files (.cmd, .bat) associated with cmd.exe (the legacy Command Prompt), PowerShell's .ps1 script files are by default not directly executable from outside PowerShell.
Instead, they are treated as documents that are by default associated with either Notepad or the (obsolescent) Windows PowerShell ISE, depending on the , and invoking them therefore opens them for editing, which applies to the following contexts:
Invoking a .ps1 file from cmd.exe
Invoking a .ps1 file from Start Menu's Run dialog, as in your case (which you can invoke with WinKey+R, for instance)
Opening (double-clicking) a .ps1 file from File Explorer or Desktop.
To execute a .ps1 script from outside PowerShell, you must therefore invoke it via PowerShell's CLI, powershell.exe for Windows PowerShell, pwsh for PowerShell (Core) 7+.
In the simplest case, using Windows PowerShell and the -File parameter, as also shown by Mathias R. Jessen in a comment; see the comments below and the linked docs for additional parameters:
# Note:
# * The effective execution policy applies; use -ExecutionPolicy Bypass to bypass.
# * Profiles are loaded; use -NoProfile to suppress.
# * The console window auto-closes when the script terminates; use -NoExit
# to keep the session open.
powershell.exe -File D:\pscript\Intune.ps1
For a comprehensive overview of PowerShell's CLI, see this post.
It is possible - though not advisable - to configure your system to execute .ps1 files by default - see this answer.

Powershell not finding PNPUTIL when script launched from shortcut

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

Running a small WMI Powershell Script

I'm trying to have a few scripts that I can map to run from my keyboard for quickly changing the monitor/screen brightness. After some searching on the internet, I found this script which works when I enter it into Powershell.
$monitor=#(gwmi WmiMonitorBrightnessMethods -ns root/wmi)[0]
$monitor.WmiSetBrightness(50,0)
After I saved it as a .ps1 file and tried running it from the file, powershell tells me: The term "path of the file" is not recognized as the name of a cmdlet, function... and so on.
I'm not familiar with Powershell at all, can someone help with what I need to add in order for the script to run properly?
By default you can't run a PowerShell script that is in the current directory without putting .\ in front of the script name, or calling the full path of the script.
This is a security feature.
If you are in the directory that contains the script, run it by executing in a PowerShell window:
.\yourscript.ps1
Where yourscript is the name of your script.
See here for more information: https://ss64.com/ps/syntax-run.html
You may also see this error if your script has spaces in its name. If that is the case, enclose the path in quotes:
.\'your script.ps1'

Alternate for esbimportutil.exe for PowerShell BizTalk

I am working on a PowerShell Deployment scripts for BizTalk. I want to import an itinerary in XML format using PowerShell. Commands available for this task is esbimportutil.exe. But this works only in Command Prompt and not in PowerShell.
The error shows is :
The term 'esbimportutil.exe' is not recognized as the name of a cmdlet, function, script file,
or operable program.
I run the PowerShell as an Administrator and even tried running the command from the source root location but still no use.
I got the solution. The problem was resolved by using a simple command:
Start-Process -FilePath "...\esbimportutil.exe" -ArgumentList $argument
The command "start-process" did the magic.

How Can I Get PowerShell to Execute MpCmdRun.exe

PowerShell runs programs such as IpConfig and WhoAmI just as cmd would. However, I am stumped trying to run MpCmdRun.exe
Clear-Host
Set-Location "C:\Program Files\Windows Defender"
Get-ChildItem
mpcmdrun.exe
Result
Error:
mpcmdrun.exe : The term 'mpcmdrun.exe' is not recognized as the name of a cmdlet, function, script file, or operable
program.
You are doing mpcmdrun.exe. You have to do .\mpcmdrun.exe as the current folder . is not in PATH in Powershell unlike in cmd.
PS:
I wonder if you read the entire message that Powershell would have spit out when you did as you said:
Suggestion [3,General]: The command MpCmdRun.exe was not found, but
does exist in the current location. Windows PowerShell does not load
commands from the current location by default. If you trust this
command, instead type ".\MpCmdRun.exe". See "get-help
about_Command_Precedence" for more details.
PPS:
The other commands ran because they were in PATH.