#script #power-shell #scripting #ps


A library for running Windows PowerShell scripts

17 releases (6 stable)

1.1.0 Apr 12, 2023
1.0.4 Aug 12, 2022
1.0.2 Apr 4, 2022
0.3.2 Feb 25, 2022
0.1.4 Nov 7, 2019

#226 in Command line utilities

Download history 1889/week @ 2023-12-11 1303/week @ 2023-12-18 755/week @ 2023-12-25 1461/week @ 2024-01-01 1153/week @ 2024-01-08 1034/week @ 2024-01-15 1319/week @ 2024-01-22 1581/week @ 2024-01-29 1363/week @ 2024-02-05 1428/week @ 2024-02-12 1346/week @ 2024-02-19 1261/week @ 2024-02-26 1599/week @ 2024-03-04 1502/week @ 2024-03-11 1635/week @ 2024-03-18 1512/week @ 2024-03-25

6,307 downloads per month
Used in 5 crates

MIT license

291 lines

Windows Powershell script runner

This crate is pretty basic. It uses std::process::Command to pipe commands to PowerShell. In addition to that there is a convenient wrapper around process::Output especially tailored towards the usecase of running Windows PowerShell commands.


I recommend that you write the commands to a *.ps file to be able to take advantage of existing tools to create the script.

This example creates a shortcut of notepad.exe to the desktop.

In script.ps

$WScriptShell=New-Object -ComObject WScript.Shell

In main.rs

use powershell_script;

fn main() {
    let create_shortcut = include_str!("script.ps");
    match powershell_script::run(create_shortcut) {
        Ok(output) => {
            println!("{}", output);
        Err(e) => {
            println!("Error: {}", e);

You can of course provide the commands as a string literal instead. Just beware that we run each line as a separate command.

The flag print_commands can be set to true if you want each command to be printed to the stdout of the main process as they're run which can be useful for debugging scripts or displaying the progress.

Use the PsScriptBuilder for better control

Instead of running a script using powershell_script::run() you can use PsScriptBuilder to configure several options:

use powershell_script::PsScriptBuilder;

fn main() {
    let ps = PsScriptBuilder::new()
    let output = ps.run(r#"echo "hello world""#).unwrap();

    assert!(output.stdout().unwrap().contains("hello world"));

Features and compatability

On Windows it defaults to using the PowerShell which ships with Windows, but you can also run scripts using PowerShell Core on Windows by enabling the core feature.

On all other operating systems it will run scripts using PowerShell core.


Right now this is only meant as a convenient wrapper for running PowerShell scripts, and I've been thinking about creating a utils crate with common tasks on Windows like creating a shortcut to a file (symlinking requires administrative privileges) but that will be better off in a separate create so this can focus on running scripts.

Any pull requests with bugfixes or efficiency improvements is greatly appreciated.

No runtime deps