Writing my first powershell script: commands are not recognized in the script - powershell

rename-item A B
When typed in the shell, and provided there is a file A at the local directory, the rename works.
When I write a one-line script, put it in a folder, export the path to this folder, go to another folder, containing a file A, and call the script, here is what happens:
'rename-item' is not recognized as an internal or external command,
operable program or batch file.
What am I missing?

When you ran the batch file, you were calling a batch file that had PowerShell commands in it. The command prompt was directing its output through Powershell, which was the above errors. Command.exe cannot run powershell cmdlets, so when it got to Rename-Item, it was not a good internal or external command, operable program or batch file.
Ping.exe is called when you Ping www.google.com for example of what is a good command or executable.
Powershell can run commands from command.exe, the reverse is not true.
You can see the same result if you open a cmd prompt and try to run powershell commands or cmdlets.:
H:\>rename-item 'rename-item' is not recognized as an internal or external command,
operable program or batch file.

Related

While creating a cordova app error is powershell is not recognized as an internal or external command, operable program or batch file

So I am creating a cordova app using the command is cordova create TestApp and the error is
Command failed: powershell (Get-CimInstance -ClassName Win32_OperatingSystem).caption
'powershell' is not recognized as an internal or external command,
operable program or batch file. I have also attached the screenshot please take a look at this.
First you should excute the command "powershell",
If fail, config system variable path "C:\Windows\System32\WindowsPowerShell\v1.0".
If your windows System32 folder have no WindowsPowerShell this folder, install powershell and config it to system variable path

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'

How to fetch an already opened cmd window and run commands in it from a batch file?

There is a bat file in which a command (nqcmd) is executed. This command only executes, when we open the batch file as an administrator. Otherwise, an error comes.
I tried to run the batch file as administrator from powershell, like this,
powershell.exe -Command "start-process -filepath C:\foo.cmd -verb runas"
It opens two cmd windows - a normal cmd window, in which the batch file executes and an error comes:
'nqcmd is not recognized as an internal or external command, operable program or batch file',
and an administrator's cmd window.
But, in this window, that batch file does not executes. We have to type the batch file's name again and then it executes.
I wanted to ask that, in a separate batch file, can we take that instance of administrator's cmd window and then run commands in it, without manually typing in that window?
Thank you.

Running Powershell script from SSIS with OnTap cmdlets throws error, but not when run from PS Cmd Line?

We are trying to run a Poweshell Script that uses the OnTap PS Modules, from SSIS, when we do, an error is issued:
Error: The term 'Connect-NaController' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
But when we run the same script from the Powershell Command Line, then the script runs just fine. So I think the scripts are fine.
So I'm wondering if the security context is different or we have to do something more explicitly in our call from SSIS?
When we call the script from SSIS we use: -ExecutionPolicy ByPass
Thanks!
In SSIS i had to set the FULL PATH of the script instead of ./scriptfile

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.