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.

Chef Pharaoh
  • 2,387
  • 3
  • 27
  • 38
  • 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??? – djangofan Aug 27 '13 at 00:19
  • 1
    I should clarify, I don't want to create a powershell script. – Chef Pharaoh Aug 29 '13 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) – NiKiZe Aug 29 '13 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...?? – Chef Pharaoh Aug 29 '13 at 22:38
  • 1
    [stackoverflow.com/a/19111810](http://stackoverflow.com/a/19111810) may be relevant for you. It is about how to combine .cmd and .ps1 into 1 file (.cmd). – robert4 Oct 01 '13 at 09:23
  • @homay2 Thx for the reference, looks good. I will definitely have to look at that more since I'm still a bit new with PowerShell. – Chef Pharaoh Oct 01 '13 at 19:08
  • Not sure why my answer was deleted... I found the solution and posted it as these other answers did NOT solve my problem. For ref, try: `powershell -command "& {&'some-command' someParam}"` or for multiple commands try: `powershell -command "& {&'some-command' someParam}"; "& {&'some-command' -SpecificArg someParam}"`. This link helped out: http://forloveofsoftware.blogspot.ca/2009/04/calling-powershell-script-in-path-with.html – Chef Pharaoh Jan 09 '15 at 18:08

4 Answers4

69

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}"
Chef Pharaoh
  • 2,387
  • 3
  • 27
  • 38
  • 2
    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` – Chef Pharaoh Aug 28 '15 at 21:18
  • 5
    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 Nowicki Jun 10 '16 at 05:47
  • 1
    @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. – Chef Pharaoh Sep 17 '16 at 11:41
  • 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"` – bnp887 Apr 16 '19 at 11:28
  • 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... – Mickaël C. Guimarães Jul 28 '23 at 14:56
  • @MickaëlC.Guimarães I haven't tried it on Windows 11, but do you need to use the `Import-Module` command as part of your command string? – Chef Pharaoh Jul 30 '23 at 06:57
13

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>"
djangofan
  • 28,471
  • 61
  • 196
  • 289
  • 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? – Chef Pharaoh Aug 28 '13 at 22:37
  • This didn't work, not to mention the -WindowStyle flag closes my command prompt... Also, I don't need the ExecutionPolicy flag since I'm using the command line directly. – Chef Pharaoh Aug 29 '13 at 15:38
  • Yes, some of the options are probably unnecessary. Just tweak depending on what you need. More info here: http://zduck.com/2012/powershell-batch-files-exit-codes/ – djangofan Aug 29 '13 at 17:33
2

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

Take a look at powershell /?

NiKiZe
  • 1,256
  • 10
  • 26
  • 1
    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... – Chef Pharaoh Aug 26 '13 at 23:57
  • 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. – NiKiZe Aug 27 '13 at 00:09
  • This syntax worked for me. One caveat I saw is it doesn't work with single quotes. – anthonybell Sep 03 '15 at 16:39
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 "&".
oldpride
  • 761
  • 7
  • 15