Run PowerShell command from command prompt (no ps1 script)

前端 未结 4 1542
花落未央
花落未央 2020-12-12 22:17

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

相关标签:
4条回答
  • 2020-12-12 22:46

    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>"
    
    0 讨论(0)
  • 2020-12-12 22:54

    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 "&".
    0 讨论(0)
  • 2020-12-12 23:12

    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}"
    
    0 讨论(0)
  • 2020-12-12 23:12

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

    Take a look at powershell /?

    0 讨论(0)
提交回复
热议问题