PowerShellLibJava

A simple library for using PowerShell from Java.

License

License

Categories

Categories

Java Languages
GroupId

GroupId

com.github.tuupertunut
ArtifactId

ArtifactId

powershell-lib-java
Last Version

Last Version

2.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

PowerShellLibJava
A simple library for using PowerShell from Java.
Project URL

Project URL

https://github.com/Tuupertunut/PowerShellLibJava
Source Code Management

Source Code Management

https://github.com/Tuupertunut/PowerShellLibJava

Download powershell-lib-java

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.tuupertunut/powershell-lib-java/ -->
<dependency>
    <groupId>com.github.tuupertunut</groupId>
    <artifactId>powershell-lib-java</artifactId>
    <version>2.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.tuupertunut/powershell-lib-java/
implementation 'com.github.tuupertunut:powershell-lib-java:2.0.0'
// https://jarcasting.com/artifacts/com.github.tuupertunut/powershell-lib-java/
implementation ("com.github.tuupertunut:powershell-lib-java:2.0.0")
'com.github.tuupertunut:powershell-lib-java:jar:2.0.0'
<dependency org="com.github.tuupertunut" name="powershell-lib-java" rev="2.0.0">
  <artifact name="powershell-lib-java" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.tuupertunut', module='powershell-lib-java', version='2.0.0')
)
libraryDependencies += "com.github.tuupertunut" % "powershell-lib-java" % "2.0.0"
[com.github.tuupertunut/powershell-lib-java "2.0.0"]

Dependencies

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
org.hamcrest : hamcrest-core jar 1.3

Project Modules

There are no modules declared in this project.

PowerShellLibJava

A simple library for using PowerShell from Java.

Usage

PowerShell.open() opens a new PowerShell session with default executable. On Windows it is powershell, and on other platforms it is pwsh from PowerShell Core. You can also specify a custom PowerShell executable, for example PowerShell.open("/usr/bin/pwsh-preview").

You can execute a PowerShell command with psSession.executeCommands(command). It will return the output of the command as a string:

try (PowerShell psSession = PowerShell.open()) {
    System.out.println(psSession.executeCommands("Write-Output 'hello Java'"));
} catch (IOException | PowerShellExecutionException ex) {
    ex.printStackTrace();
}
hello Java

You can also execute multiple lines of commands at once:

try (PowerShell psSession = PowerShell.open()) {
    System.out.println(psSession.executeCommands(
            "for ($i = 1; $i -le 5; $i++) {",
            "    Write-Output $i",
            "}"));
} catch (IOException | PowerShellExecutionException ex) {
    ex.printStackTrace();
}
1
2
3
4
5

If your PowerShell code has parameters that are dynamically read from Java strings, they might contain characters that have special meaning in PowerShell (kind of like SQL injection). You can sanitize your input with PowerShell.escapePowerShellString(parameter):

String param = "thi's won't bre;ak' the' code";

try (PowerShell psSession = PowerShell.open()) {
    System.out.println(psSession.executeCommands("Write-Output " + PowerShell.escapePowerShellString(param)));
} catch (IOException | PowerShellExecutionException ex) {
    ex.printStackTrace();
}
thi's won't bre;ak' the' code

If there is an error on executing the command, a PowerShellExecutionException is thrown:

try (PowerShell psSession = PowerShell.open()) {
    System.out.println(psSession.executeCommands("this is not a valid command"));
} catch (IOException | PowerShellExecutionException ex) {
    ex.printStackTrace();
}
tuupertunut.powershelllibjava.PowerShellExecutionException: Error while executing PowerShell commands:
this : The term 'this' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
...

Requirements

OS: Works on every platform that has PowerShell available.

PowerShell: On Windows 7 and newer, Windows PowerShell is installed by default and this library should work with it out of the box. On other platforms, you need to have PowerShell Core (https://github.com/PowerShell/PowerShell) installed.

Java: Java 8 or higher is required.

Maven Central

https://mvnrepository.com/artifact/com.github.tuupertunut/powershell-lib-java

groupId: com.github.tuupertunut

artifactId: powershell-lib-java

Changelog

1.1.0

  • Added support for PowerShell Core on all platforms.
  • Added support for custom PowerShell executables.

1.1.1

  • Fixed open PowerShell session blocking program exit.
  • Fixed PowerShell process not terminating on close() on some platforms.
  • Improved error messages when PowerShell process is terminated before it should.

2.0.0

  • Added module name for the library.
  • Fixed deadlock when a script threw an exception using throw.
  • Fixed error on startup when security manager is present.
Breaking changes
  • Error messages coming from PowerShell now look a bit different.

Versions

Version
2.0.0
1.1.1
1.1.0
1.0.0