Trouble Running PowerShell Scripts from CMD

0
8
Asked By TechNinja42 On

I'm trying to run a couple of PowerShell scripts from a CMD file on an HMI panel. My CMD file includes commands to run PowerShell and execute scripts from a USB stick, but I'm running into issues. I've set the execution policy to unrestricted, but I'm getting errors saying the scripts can't be found or have an invalid extension.

When I open PowerShell as an admin and navigate to the USB stick, the scripts run perfectly. I used the "%~dp0" variable to get the directory of the CMD file to reference the scripts, but it's still not working. Last time I tried calling the scripts directly from the CMD file, I had the same errors.

Could someone help me figure out what's going wrong when I try to launch the PowerShell files directly from CMD?

5 Answers

Answered By USBWhiz On

If you're navigating to external drives, be sure to run these commands while specifying the drive letter. For example: `powershell -noexit -File "D:windowsPostInstall.ps1"`. Since you're using a USB, commands that expect local paths might fail to locate the scripts if incorrectly addressed.

Answered By PowerShellGuru On

Double-check how you're calling the scripts, especially if your path has spaces. Wrap the path in quotes like this: `powershell -noexit -File "%~dp0windowsPostInstall.ps1"`. This should help resolve the issue where it can't find your script.

TechNinja42 -

I tried that, but I still see two new windows popping up and disappearing quickly. It’s frustrating!

PowerShellGuru -

It sounds like the scripts might be exiting immediately. Maybe remove the `-noexit` option for now to see if they run at all.

Answered By BatchBrawler On

It might be an admin problem—make sure you're running both the CMD file and PowerShell as administrators. Sometimes, permissions can mess up execution, even if it seems like you're doing everything right.

Answered By ScriptMaster88 On

It sounds like you might be overcomplicating things a bit. Could you use PowerShell 7 instead of 5.1? If so, you could try running it with the command `pwsh -noprofile -noexit -command ".script.ps1"`. This approach allows you to run your scripts directly from the CMD prompt. Also, remember that '$PSScriptRoot' can help get the script directory in PowerShell.

TechNinja42 -

Thanks for the tip! I modified my CMD file to check for PowerShell 7 and it works better now. I still have a problem with making sure it runs without an existing network, though.

ScriptMaster88 -

Glad to hear it helped! Let me know if you need more tips on handling network setups.

Answered By CommandLineKiddo On

You should specify the drive letter of your USB stick directly. If your USB is usually the D: drive, try using that in your command: `powershell -noexit -File "D:InstallWifi.ps1"`. Just make sure your script is located on the root of the USB drive for this to work.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.