How to Configure Jenkins to Use PowerShell

Photo of author

By Victor Ashiedu

Published

Are you transitioning from a Windows SysAdmin to a DevOps Engineer and want to continue using your PowerShell skills? One of the ways you can do this is to configure Jenkins to use PowerShell.

Overview

When you create a Jenkins job, you can run Linux Shell or Windows bash commands to automate tasks. However, running PowerShell commands is not included by default.

Default Jenkins job build step does not include PowerShell

Fortunately, there is a way to set up PowerShell to display as an option in your Jenkins jobs. This guide covers the steps to run PowerShell commands as part of your Jenkins build steps.

This guide assumes that you’ve installed Jenkins. If you have not installed Jenkins before you configure it to use PowerShell, you must install it first”. Read our guide – How to Install Jenkins on Ubuntu with a Docker Container.
I am using the Jenkins FreeStyle project for the demo in this guide. Note that in production, due to the limitations of this job type, you may not use it.

Follow the steps below to start using PowerShell in your Jenkins job automation:

Step 1: Install the Jenkins PowerShell Plugin

  1. Login to your Jenkins server and click Manage Jenkins => Plugins
How to Configure Jenkins to Use PowerShell: Step 1: Install the Jenkins PowerShell Plugin
  1. To install the Jenkins PowerShell plugin, click the “Available plugins” node on the left pane. Then, type “powershell” on the search bar, finally, check the box next to the PowerShell plugin and click “install.”
set up Jenkins to Use PowerShell
  1. Finally, wait for the plugin to install, scroll down, and click “Go back to the top page.”

Step 2: Test Jenkins PowerShell Plugin

After completing the first step in configuring Jenkins to use PowerShell, let’s test it. To accomplish this, we would create a Jenkins FreeStyle job and add a PowerShell command.

  1. To create a FreeStyle project, from the Jenkins Dashboard, click “+ New item” or “Create a job +”.
Configure Jenkins to Use PowerShell
  1. After that, give the Jenkins job a name, select Freestyle project, and click OK. Jenkins will create the job and open it for editing.
After that, give the Jenkins job a name, select Freestyle project, and click OK
  1. Scroll down to the “Build Steps” section of the job and click the “Add build step” drop-down.
How to Configure Jenkins to Use PowerShell
  1. As the screenshot below confirms, PowerShell is now available as an option! Select it.
  1. Finally, on the PowerShell field, enter a simple command – Get-ChildItem /etc/ – and click the Save button.

6. Back on the Jenkins project, click the “Build Now” button, then select “Build scheduled.”

The build will fail. To see the build details, scroll down to “Build History” and click one of the jobs.

set up Jenkins to Use PowerShell

Looking at the job’s build console output reveals why it failed. The job was trying to run the PowerShell (pwsh) command but PowerShell is NOT installed on the Jenkins server.

This is why the job failed. To fix this problem, we need to install PowerShell on the Jenkins server.

The next section explains the steps to accomplish this.

Step 3: Install PowerShell on Your Jenkins Server

The final step to set up Jenkins to use PowerShell is to install PowerShell on the Jenkins host server.

If Jenkins is running directly on the Ubuntu server, you must install PowerShell on the server. However, if you started Jenkins as a Docker Container, you must install PowerShell on the container.

In my case, my Jenkins server is running as a Docker container, so I’ll install PowerShell on the container. Here is the step-by-step walkthrough:

  1. Log in to the Jenkins Docker container as root by running the “Docker exec” command below:
The Docker exec command requires the container ID. The “docker ps” command returns this information.
sudo docker ps #returns the container ID
sudo docker exec -u 0 -it 977a52341f9a bash #logs into the container as root

The “-u 0” parameter signs you in as root. On the other hand, the “-it” parameter signs you in “interactively.”

The string, 977a52341f9a is my Jenkins container’s ID while adding “bash” to the end opens the BASH prompt. Here is the output of the command.

The part of the screenshot I labeled (1) shows that I’m signed in to my Docker container – 977a52341f9a is my container’s ID. Meanwhile, the # sign indicates that I’m signed in as root.

  1. To install PowerShell, we must first determine the Linux Distro running on the Docker container. To get this information, execute the command below:
cat /etc/os-release

The command confirms that my container is running the Debian Linux Distro.

This information will help me determine the Microsoft script to install PowerShell. Therefore, since my container’s Distro is Dabien, I will use the Installing on Debian script.

Here is the modified script – I removed “sudo” as we do not require that on a container – we signed in as root!

###################################
# Prerequisites

# Update the list of packages
apt-get update

# Install pre-requisite packages.
apt-get install -y wget

# Download the PowerShell package file
wget https://github.com/PowerShell/PowerShell/releases/download/v7.4.0/powershell_7.4.0-1.deb_amd64.deb

###################################
# Install the PowerShell package
dpkg -i powershell_7.4.0-1.deb_amd64.deb

# Resolve missing dependencies and finish the install (if necessary)
apt-get install -f

# Delete the downloaded package file
rm powershell_7.4.0-1.deb_amd64.deb

# Start PowerShell
pwsh

Before you proceed, run the “apt update” command to update the package manager.

Then, copy the above script, paste it into your container’s console, and click Enter.

When you run the command, you’ll be prompted to confirm the installation. Type Y and hit the enter key.

When the script finishes running it opens the PowerShell console, confirming that we have successfully installed PowerShell on our Docker container.

  1. To exit PowerShell and return to the container’s shell, execute the “exit” command. Additionally, to exit the container, execute “exit” again.

Step 4: Rebuild the Jenkins Job and Test

Now that we have installed PowerShell on the Jenkins server (the Docker container in my example), we can rebuild the job and see if it runs successfully.

  1. Log in to the Jenkins server via the browser and open the PowerShell project.
  2. After that, click the “Build Now” button, then select “Build scheduled.”
  1. When the build job finishes running, scroll down to the “Build History” section. The build should now be successful. Click on the last build job.
  1. Finally, click on “Console Output”

The details of the build jobs confirmed that the “Get-ChildItem” command ran successfully!

Conclusion

Although the command we used for this demo is simple, it confirms that after setting up Jenkins to use PowerShell, you can automate Jenkins jobs using PowerShell commands.

We hope you improved your DevOps skills one notch by reading this guide. We would love to hear your thoughts.

Use the Comments form at the end of this page to share your thoughts or ask questions about this topic or any DevOps question.

Alternatively, you can respond to the “Was this page helpful?” question below.

About the Author

Photo of author

Victor Ashiedu

Victor is the founder of InfoPress Media, publishers of Ilifeguides and Itechguides. With 20+ years of experience in IT infrastructure, his expertise spans Windows, Linux, and DevOps. Explore his contributions on Itechguides.com for insightful how-to guides and product reviews.

Related Articles

Get in Touch

We're committed to writing accurate content that informs and educates. To learn more, read our Content Writing Policy, Content Review Policy, Anti-plagiarism Policy, and About Us.

However, if this content does not meet your expectations, kindly reach out to us through one of the following means:

  1. Respond to "Was this page helpful?" above
  2. Leave a comment with the "Leave a Comment" form below
  3. Email us at [email protected] or via the Contact Us page.

Leave a comment