Questions tagged [powershell]

PowerShell is a command line and scripting utility for use with Windows.

Questions about Windows administration tasks involving PowerShell should be asked on Super User. On-topic questions are for writing scripts ONLY

Windows PowerShell

is an interactive shell and scripting language originally included with Microsoft Windows 7 and Microsoft Windows Server 2008 R2 and above. It includes a command-line shell (Windows PowerShell) for interactive use, an underlying scripting environment to run scripts away from the command-line and a GUI script editing / debugging environment (Windows PowerShell ISE). See: Getting Started with Windows PowerShell.

As a language, has syntax for literal arrays and hashtables, support for regexes, pattern matching and string expansion. It's built on the .NET framework so it has Unicode support, can be locale/culture aware, and can access .NET framework methods directly.

As a command-line shell, it is designed around cmdlets named in the form {Verb}-{Noun}, intending that the same kinds of commands work across many domains. E.g. Get-Date returns the current date, and Get-Process returns an array of objects representing running processes which can be piped to other commands that work with process objects. Many commands and keywords have short aliases to reduce typing.

As a system management environment, Windows components and Microsoft products have been extended to provide native PowerShell interfaces as part of creating a unified managing system for Windows systems, including:

Third-party vendors also offer PowerShell integration, including:

takes the Unix idea of piping text between programs and manipulating text, and enhances it by piping .NET object instances around. Because objects carry type information (e.g. dates and times), and complex state (e.g. properties and methods, hashtables, parsed XML data, live network sockets) this makes many tasks easy that would be difficult or impractical to do by passing text between programs.

Along with interacting with the PowerShell console or the PowerShell ISE, there are also several 3rd party IDE options including Sapien's PrimalScript ISE.

Example Usage

# List all processes using > 100 MB of PagedMemory in descending sort order (v3_    
C:\PS> Get-Process | Where PagedMemorySize -GT 100MB | Sort -Descending

PowerShell can handle numbers and arithmetic

C:\PS> (98.6 - 32) * 5/9 37

Production orientation allows experimentation and confirmation

C:\PS> Get-ChildItem C:\Users\John *.bak -r | Where {$_.LastWriteTime -gt (Get-Date).AddDays(-7)} | Remove-Item -WhatIf What if: Performing operation "Remove File" on Target "C:\Users\John\foo.bak"

C:\PS> Get-Process iexp* | Stop-Process -Confirm

Confirm Are you sure you want to perform this action? Performing operation "Stop-Process" on Target "iexplore (7116)". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is Y):

Common Gotchas

Executing EXEs via a path with spaces requires quoting the path and the use of the call operator - &

C:\PS> & 'C:\Program Files\Windows NT\Accessories\wordpad.exe'

Calling PowerShell functions does not require parenthesis or comma separated arguments. PowerShell functions should be called just like a cmdlet. The following examples demonstrates the problem caused by this issue e.g.:

C:\PS> function Greet($fname, $lname) {"My name is '$lname', '$fname' '$lname'"}
C:\PS> Greet('James','Bond') # Wrong way to invoke this function!!
My name is '', 'James Bond' ''

Note that both 'James' and 'Bond' are packaged up as a single argument (an array) that is passed to the first parameter. The correct invocation is:

C:\PS> Greet James Bond
My name is 'Bond', 'James' 'Bond'

Note that in PowerShell 2.0, the use of Set-StrictMode -version 2.0 will catch this type of problem.

Extensible functionalities

One of the great features of Powershell is its extensibility: we can add functionality by importing modules which are package of cmdlets, functions and aliases specialised on a particular domain (such as database administration, virtual machine administration etc.).

Here are the most commonly used modules by the community :

  • PSReadLine PSReadLine replaces the command line editing experience in PowerShell.exe "PSReadLine replaces the command line editing experience in PowerShell.exe for versions 3 and up." Included in Windows 10.

  • Powertab
    "PowerTab offers enhanced tab expansion for PowerShell."

  • PSCX
    "PowerShell Community Extensions (PSCX) is aimed at providing a widely useful set of additional cmdlets, providers, aliases, filters, functions and scripts for Windows PowerShell that members of the community have expressed interest in but haven't been added to PowerShell yet."

Resources

23 questions
8
votes
2 answers

Application to run PowerShell scripts

I've created a number of scripts in PowerShell that require a number of parameters to be manually entered. Unfortunately, until we have our Jenkins server setup to support these additional tasks I need to pass these scripts to other…
KJQ
  • 83
  • 3
8
votes
2 answers

Ansible sporadic errors with Windows machines

I'm running into some on and off issues when using windows hosts in my Ansible playbooks. I'm running Ansible 2.3 with pywinrm 0.2.2 installed. I'm using basic authentication with the local Administrator user. Sometimes I receive this issue when I…
Asaf Haim
  • 161
  • 2
5
votes
2 answers

How to automate Powershell's interactive Get-Credential method?

I am trying to write a python script which automates the powershell's Get-Credential method. Here is something i wrote: import subprocess COMMAND_LINE = 'powershell' powershell = subprocess.Popen(COMMAND_LINE, shell=True, …
Vivek Lohiya
  • 51
  • 1
  • 2
5
votes
2 answers

Azure Devops Pipeline variables in Powershell

I've tried to figure this out and I can't see why my ipaddress is not getting set. I've checked code samples and they all suggest I am doing everything correct. ip variable defined here: This is my powershell script: # Write your PowerShell…
4
votes
1 answer

Configure Docker to use SSL for a private registry on Windows 10?

Trying to use a customer registry from a Windows 10 system - a native Docker client in PowerShell. PS > docker --version Docker version 17.06.0-ce, build 02c1287 PS > docker pull REPO_URL/foo-bar:0.1 Error response from daemon: get…
Ta Mu
  • 6,792
  • 5
  • 43
  • 83
4
votes
1 answer

I'm trying to run `vagrant provision` on PowerShell Window for Ansible playbook

Here is what it replies: Windows is not officially supported for the Ansible Control Machine. Please check https://docs.ansible.com/intro_installation.html#control-machine-requirements What should I do? Thank you
3
votes
1 answer

Why is my build script only failing when it is run in a vNext build?

I am running a build process on an on-prem server with TFS 2017 and vNext. The build works, but it takes 20+ minutes. This is an application that does msbuild, ngc, yarn, and karma. I would be totally fine with leaving it the way it is except for…
Aaron
  • 41
  • 3
2
votes
0 answers

Running a powershell script via Jenkins on a Linux host

I have a powershell script cloned into my workspace from a bitbucket repository. If I login to the Jenkins server cli I can run it by doing cd /var/lib/jenkins/workspace/powershell pwsh ./psscript.ps1 This runs fine as expected, but when I try to…
CEamonn
  • 213
  • 2
  • 3
  • 7
2
votes
2 answers

Powershell script in jenkins is hanging

I'm building a process to automate deploys using Jenkins and I have an issue getting the code on the server I have a powershell script on the deploy server that all it does is do an hg update command. $ciPwd = ConvertTo-SecureString…
Eman
  • 123
  • 1
  • 6
2
votes
2 answers

Ansible: How to run local powershell script on remote hosts

I'm trying to write a play that will take a locally stored powershell script from my Ansible server and run it on Windows hosts. I have been using the "win_shell" module to run scripts placed inside my playbook files, but for testability I want to…
James T Snell
  • 289
  • 1
  • 2
  • 9
2
votes
4 answers

Why will my container run in Docker, but will not run in Kubernetes?

Background I have a container that I have defined with an CMD and an ENTRYPOINT in Docker. When I run the following command my container runs with no issues whatsoever. docker run --rm -ti custom-container:latest Here is the definition for my…
2
votes
1 answer

Azure Powershell DSC or Ansible

I wanted to find out what are you using for Desired State Configuration on your end. I am thinking to start introducing Azure Powershell DSC or Ansible for DSC of our on-prem environment and cloud environment. Would you have any recommendations? I…
bluehatgeek
  • 21
  • 1
  • 2
1
vote
1 answer

How to pass variables between build/deploy tasks in a TaskGroup in VSTS 16.131.28601.4

I have a multi-configuration web application I'm using parallel deployments, leveraging Task Groups for reusing my deployment code. I've been using Powershell tasks and the ##vso[task.setvariable variable] but those only are readable by other tasks…
Pxtl
  • 111
  • 3
1
vote
1 answer

Creating a custom alpine iso - including open-vm-tools

I want to create a custom Alpine Linux image. I want to take the Virtual ISO version (https://dl-cdn.alpinelinux.org/alpine/v3.20/releases/x86_64/alpine-virt-3.20.1-x86_64.iso) extend it with some packages and do some configuration stuff. Then I…
Bernte
  • 11
  • 3
1
vote
1 answer

REST API: create project with powershell returns unspecified internal error

I'm creating a a project with the code below. The response is always an unspecified error. Invoke-RestMethod : {"$id":"1","innerException":null,"message":"TF400898: An Internal Error Occurred. Activity…
Brtrnd
  • 113
  • 4
1
2