0

I need to run powershell/powershell.exe from commandline, in windows server 2016 which is a vm im vSphere, but it does not work, the error is as follows: enter image description here

the goal I want to achive is to run powershell from remote "Ansible/pywinrm/winrm". and they use powershell commands. therefrore, alternatives are not helping because I can not replace them.

I will appriciate if anyone can help me to go forward.

what I done or checked:

  1. installed powershell 7, after that this command: "pwsh" works, but "powershell" command not. 2.check environment variables, and add powershell there:
$env: PATH
$env: psmodulePATH
  1. tried this link:Microsoft-starting powershell this path is given here: %SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe and it is instresting because when I run powershell direct from here, the there is more information after error, and when I run the command as bellow it works only on this powershell!
./powershell

enter image description here

and if I run the powershell with fullpath from a windows powershell it works as well enter image description here

2
  • From the look of that prompt (eg "PS <folder path>") in your screenshots, it looks like you're already running Powershell, as you're running those commands within a Powershell prompt. So you're essentially trying to run powershell from within powershell?!? Commented May 2, 2023 at 10:01
  • yes powershell has itself the powershell command Commented May 2, 2023 at 11:18

1 Answer 1

1

Try running C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.

If it works, this means your %Path% is corrupt.

Try using the following command to fix it:

$sys_path = "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\"
Set-ItemProperty -Path $sys_path -Name Path -Type String -Value $((Get-ItemProperty -Path $sys_path -Name Path).Path + ';C:\Windows\System32\WindowsPowerShell\v1.0')

If it doesn't work, then it means your Windows system is corrupt.

Run the following commands to fix your OS:

dism /online /cleanup-image /restorehealth
sfc /scannow

Reboot your computer then try to fix the %PATH% using my command.

Sign up to request clarification or add additional context in comments.

2 Comments

I think you are right but the problem still exist. I done both of your suggestions.
it is funny now! :)) I add this path you told to the path, it works and then when I close the windows it does not work again. windows will automatically remove the path from environment each time(I run as admin)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.