1

I want to call PowerShell command from cmd. For example, let it be get-content.

PowerShell.exe Command Line Interface

OK, my case will be

powershell -command <string> [<CommandParameters>]

In my case it will looks like

powershell -command "& {get-content <???>}" <filepath>

But it doesn't works. I can't understand how to pass cmd arg <filepath> into the command string "& {get-content <???>}".

3
  • 1
    powershell -command " #PasteCodeHere ". So, in your case, it should be powershell -command "Get-Content 'c:\temp\textfile.txt' ". No need to use the &, only when running against a file. powershell -command "& 'C:\Temp\myps.ps1'" Commented Mar 4, 2021 at 3:18
  • But I need to pass filepath as argument from cmd. Is there only one way to do it - use external ps1 file? Looks lame. Commented Mar 4, 2021 at 4:17
  • use a BAT/CMD environment variable [OR any other variable that works in the shell] in the argument string. ///// a better solution would be to run things entirely in PoSh ... most utility apps can be called from inside powershell. Commented Mar 4, 2021 at 11:45

1 Answer 1

2

According to @Abraham Zinala you can write a cmd file test.cmd with the following :

powershell -command "Get-Content 'c:\tmp\toto.txt' "

If you want to write :

.\test.cmd c:\tmp\toto.txt

You can put into your cmd file :

powershell -command "Get-Content '%1' "
Sign up to request clarification or add additional context in comments.

1 Comment

So, the answer is "no way". Anycase if I want to run ps-script from cmd I need to create intermediate script file. So sad :(

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.