-2

Possible Duplicate:
How to execute powershell commands from a batch file?

I want to execute the below powershell statement from a batch file with out creating a ps1 file

if([System.Diagnostics.EventLog]::SourceExists("dfgdjg") -eq $false){[System.Diagnostics.EventLog]::CreateEventSource("dfgdjg","dfgdjgLogs");}
else{write("Event Log already exists");}

Is it possible to do so?

Community
  • 1
  • 1
user602737
  • 1,285
  • 4
  • 12
  • 11

2 Answers2

3

In general you can do:

 @powershell -command "yourpowershellcommand"

You can use powershell.exe directly, but I would recomment you to use the above form of @powershell

manojlds
  • 259,347
  • 56
  • 440
  • 401
  • Guys it doesnt work. I tried doing something like this powershell -command 'if([System.Diagnostics.EventLog]::SourceExists("dfgdjg") -eq $false){[System.Diagnostics.EventLog]::CreateEventSource("dfgdjg","dfgdjgLogs");}else{write("Event Log already exists");}' – user602737 May 30 '11 at 20:52
  • 1
    The `@` only causes the command not to be echoed if `echo` is on. In most batch files it's off anyway. There is no functional difference between the invocation with or without the `@`. – Joey May 31 '11 at 00:07
  • When you have your script in a file, the following works fine: `@powershell Invoke-Expression Hello_World.ps1` – Dominique May 10 '17 at 07:33
1

The command line help powershell.exe /? covers this:

-Command
    Executes the specified commands (and any parameters) as though they were
    typed at the Windows PowerShell command prompt, and then exits, unless
    NoExit is specified. The value of Command can be "-", a string. or a
    script block.

    If the value of Command is "-", the command text is read from standard
    input.

    If the value of Command is a script block, the script block must be enclosed

    in braces ({}). You can specify a script block only when running PowerShell.exe

It shows an example at the end:

EXAMPLES
    PowerShell -PSConsoleFile SqlSnapIn.Psc1
    PowerShell -version 1.0 -NoLogo -InputFormat text -OutputFormat XML
    PowerShell -Command {Get-EventLog -LogName security}
    PowerShell -Command "& {Get-EventLog -LogName security}"
bobbymcr
  • 22,445
  • 3
  • 50
  • 65
  • Guys it doesnt work. I tried doing something like this powershell -command 'if([System.Diagnostics.EventLog]::SourceExists("dfgdjg") -eq $false){[System.Diagnostics.EventLog]::CreateEventSource("dfgdjg","dfgdjgLogs");‌​}else{write("Event Log already exists");}' – user602737 May 30 '11 at 20:53
  • 1
    @user: Read the help entry again. Then surround your command with `&{...}`. And then try again. – Joey May 31 '11 at 00:06