API to easily execute PowerShell commands and scripts from Java.
- Windows
- Linux (with installed PowerShell)
- MacOS (with installed PowerShell)
Call PowerShell commands or scripts like this:
PowerShellExecutor executor = PowerShellExecutor.instance();
System.out.println("PowerShell runtime version " +
executor.version().orElseThrow(() -> new RuntimeException("No PowerShell runtime available")));
System.out.println("Execute command: ");
String output = executor.execute("Write-Host Hello PowerShell!").getStandardOutput();
System.out.println(" output = " + output);
Map<String, String> arguments = Collections.singletonMap("name", "PowerShell");
System.out.println("Execute script as file: ");
output = executor.execute(Paths.get(".\\src\\test\\resources\\test.ps1"), arguments).getStandardOutput();
System.out.println(" output = " + output);
System.out.println("Execute script from classpath: ");
output = executor.execute(PowerShellTestApplication.class.getResourceAsStream("/test.ps1"), arguments).getStandardOutput();
System.out.println(" output = " + output);
To use jpse in your project you can add the dependency from maven central to your software project management tool:
In Maven just add the following dependency to your pom.xml:
<dependency>
<groupId>com.github.frimtec</groupId>
<artifactId>jpse</artifactId>
<version>1.3.3</version>
</dependency>