47

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.

7
  • 1
    On one hand you say "I don't really want to create a script" and on the other hand you say "if I can just run one or two commands from a batch file". So: which is it??? Commented Aug 27, 2013 at 0:19
  • 1
    I should clarify, I don't want to create a powershell script. Commented Aug 29, 2013 at 16:07
  • And Get-AppLockerFileInformation works fine for you when running from a new fresh and clean powershell prompt? What OS? (tested Get-App.. in Win7 and there is no such cmdlet in that case so get the same error message) Commented Aug 29, 2013 at 20:08
  • I first use the Import-Module AppLocker command to load the library. I'm using Windows Embedded 7. Maybe I need to combine the 2 calls, import the library then run the command...?? Commented Aug 29, 2013 at 22:38
  • 1
    stackoverflow.com/a/19111810 may be relevant for you. It is about how to combine .cmd and .ps1 into 1 file (.cmd). Commented Oct 1, 2013 at 9:23

4 Answers 4

68

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}"
Sign up to request clarification or add additional context in comments.

6 Comments

Here's another useful trick, you can run a ps1 script from the command prompt and pass in args like so: powershell.exe -File "C:\myfile.ps1" arg1 arg2 arg3 or powershell.exe -File "C:\myfile.ps1" -Some-Command arg1 -Another-Command arg2
The command powershell -command "& {&'Import-Module' AppLocker}"; "& {&'Set-AppLockerPolicy' -XmlPolicy myXmlFilePath.xml}" runs these 2 PS commands in separate PS sessions, i.e. it is not applicable If you need to share some variable between 2 commands. The solution is as follows: powershell -command "& {$someVar = 'test'; &'Write-Host' $someVar}"
@Maciej I don't think it runs in different PS sessions otherwise the second part of this specific command would fail since I need that module loaded from the first part of the command. I disagree. But, there may some value in sharing variables if needed, as you mention.
Great answer. Small extension: I needed to start the powershell command in a new window from the command prompt and found that the following worked: start powershell -Command "& cd C:\somedir; npm start"
It doesn't work for me : using Windows 11 in 2023. I get error "The term [...] is not recognized as a cmdlet, function, script file etc...
|
12

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>"

3 Comments

As soon as I get a chance I will try this out. There seems to be a lot of flags in there though, would it be the same if I want to use Set-AppLockerPolicy and New-AppLockerPolicy?
This didn't work, not to mention the <code>-WindowStyle</code> flag closes my command prompt... Also, I don't need the <code>ExecutionPolicy</code> flag since I'm using the command line directly.
Yes, some of the options are probably unnecessary. Just tweak depending on what you need. More info here: zduck.com/2012/powershell-batch-files-exit-codes
2

Maybe powershell -Command "Get-AppLockerFileInformation....."

Take a look at powershell /?

3 Comments

That's what I've been trying but I don't seem to be getting the syntax quite right or something. When I run the command directly from the powershell it works fine though...
Ok, Do you get any error or something else? Please edit your question and provide more information. What have you tried and what does not work etc.
This syntax worked for me. One caveat I saw is it doesn't work with single quotes.
0

This works from my Windows 10's cmd.exe prompt

powershell -ExecutionPolicy Bypass -Command "Import-Module C:\Users\william\ps1\TravelBook; Get-TravelBook Hawaii"

This example shows

  1. how to chain multiple commands
  2. how to import module with module path
  3. how to run a function defined in the module
  4. No need for those fancy "&".

1 Comment

Yes, to run scripts you need to change your local execution policy....

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.