Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run PowerShell command from command prompt (no ps1 script)

I'm looking for a way to run just a couple PowerShell commands from the command prompt. I don't want to create a script for this since it's just a couple commands I need to run and since I don't really know how to script with PowerShell.

Here is the command I'm trying to use to start with:

Get-AppLockerFileInformation -Directory <folderpath> -Recurse -FileType <type> 

I don't really want to create a script for this as it would be much easier if I can just run one or two commands from a batch file with the rest of the stuff.

EDIT: Here is what I've tried so far.

1)

powershell -Command "Get-AppLockerFileInformation....." Error: The term 'Get-AppLockerFileInformation is not recognized as the name of a cmdlet, function, script file, or operable program.... 

2)

powershell -Command {Get-AppLockerFileInformation.....} 

No error with this way but I don't get anything back. If I use the Set-AppLockerPolicy... nothing happens.

3)

powershell -Command "{Get-AppLockerFileInformation.....}" Error: The term 'Get-AppLockerFileInformation is not recognized as the name of a cmdlet, function, script file, or operable program.... 

4)

powershell -Command "& {Get-AppLockerFileInformation.....}" Error: The term 'Get-AppLockerFileInformation is not recognized as the name of a cmdlet, function, script file, or operable program.... 

5)

powershell "& {Get-AppLockerFileInformation.....}" Error: The term 'Get-AppLockerFileInformation is not recognized as the name of a cmdlet, function, script file, or operable program.... 

6)

powershell -ExecutionPolicy Bypass -NoLogo -NoProfile -Command {Get-AppLockerFileInformation....} 

No error but nothing happens.

7)

powershell -ExecutionPolicy Bypass -NoLogo -NoProfile -Command "Get-AppLockerFileInformation...." 

No error but nothing happens.

like image 425
Chef Pharaoh Avatar asked Aug 26 '13 23:08

Chef Pharaoh


People also ask

Can I Run PowerShell command from cmd?

To start a Windows PowerShell session in a Command Prompt window, type PowerShell . A PS prefix is added to the command prompt to indicate that you are in a Windows PowerShell session.

How do I Run a PowerShell script without the console?

run with a hidden window flag to launch cmd.exe /c powershel.exe -file c:\script. ps1. When powershell is called from cmd it will run in the existing cmd window which is already hidden by wscript.exe //b /nologo c:\launcher. vbs.

How do you execute a .ps1 file from cmd?

If you would like to run a PowerShell script in CMD, you'll need to execute it by calling the PowerShell process with the -File parameter, as shown below: PowerShell -File C:\TEMP\MyNotepadScript. ps1.


2 Answers

Here is the only answer that managed to work for my problem, got it figured out with the help of this webpage (nice reference).

powershell -command "& {&'some-command' someParam}" 

Also, here is a neat way to do multiple commands:

powershell -command "& {&'some-command' someParam}"; "& {&'some-command' -SpecificArg someParam}" 

For example, this is how I ran my 2 commands:

powershell -command "& {&'Import-Module' AppLocker}"; "& {&'Set-AppLockerPolicy' -XmlPolicy myXmlFilePath.xml}" 
like image 176
Chef Pharaoh Avatar answered Oct 07 '22 03:10

Chef Pharaoh


Run it on a single command line like so:

powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile    -WindowStyle Hidden -Command "Get-AppLockerFileInformation -Directory <folderpath>    -Recurse -FileType <type>" 
like image 35
djangofan Avatar answered Oct 07 '22 03:10

djangofan