Questions tagged [powershell]

PowerShell is a cross-platform command line and scripting utility from Microsoft. Use this tag for questions about writing and executing PowerShell scripts ONLY. Programming questions specific to the cross-platform version PowerShell Core (Windows, macOS, and Linux) should be tagged [powershell-core]. Questions about system administration should be asked on Super User or Server Fault.

Questions about Windows administration tasks involving PowerShell should be asked on Super User or Server Fault. 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, and 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 third-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 EXE files 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.

PowerShell Profiles

Another common issue when moving files from a user machine into a production environment is the profile setting discrepancy. A user's machine might have a profile.ps1 defined inside this folder

%UserProfile%\Documents\WindowsPowerShell

The profile file is used to define certain commands are automatically executed prior to running a script. Common commands might include adding a PowerShell snap-in. To minimize this type of confusion between environments, it is recommended to run tests with the PowerShell command line via the -NoProfile flag. This will ensure the profile script is not executed.

More information on profiles can be found here.

Extensible functionalities

One of the great features of PowerShell is its extensibility: we can add functionality by importing modules which are a package of cmdlets, functions, and aliases specialized 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."

PowerShell Core

Since 2016, an open-source and cross-platform (Windows, macOS, and Linux) version of PowerShell has been in alpha and then beta state. Achieving general availability in 2018, PowerShell Core is built on the .NET Core Framework. Questions about functionality specific to PowerShell Core should be tagged .

Resources

114499 questions
214
votes
11 answers

The response content cannot be parsed because the Internet Explorer engine is not available, or

I need to download a channel 9 series using powershell, however the scripts I have tried have errors: This script $url="https://channel9.msdn.com/blogs/OfficeDevPnP/feed/mp4high" $rss=invoke-webrequest -uri $url…
Luis Valencia
  • 32,619
  • 93
  • 286
  • 506
213
votes
6 answers

PowerShell: How do I convert an array object to a string in PowerShell?

How can I convert an array object to string? I tried: $a = "This", "Is", "a", "cat" [system.String]::Join(" ", $a) with no luck. What are different possibilities in PowerShell?
jrara
  • 16,239
  • 33
  • 89
  • 120
212
votes
15 answers

Get full path of the files in PowerShell

I need to get all the files including the files present in the subfolders that belong to a particular type. I am doing something like this, using Get-ChildItem: Get-ChildItem "C:\windows\System32" -Recurse | where {$_.extension -eq ".txt"} However,…
Gagan
  • 5,416
  • 13
  • 58
  • 86
211
votes
21 answers

How do I output text without a newline in PowerShell?

I want my PowerShell script to print something like this: Enabling feature XYZ......Done The script looks something like this: Write-Output "Enabling feature XYZ......." Enable-SPFeature... Write-Output "Done" But Write-Output always prints a…
Amit G
  • 5,165
  • 4
  • 28
  • 29
207
votes
11 answers

How do I get the localhost name in PowerShell?

How do I get the localhost (machine) name in PowerShell? I am using PowerShell 1.0.
George2
  • 44,761
  • 110
  • 317
  • 455
200
votes
14 answers

Why is my locally-created script not allowed to run under the RemoteSigned execution policy?

Since this question continues to attract responses that are either refuted by the question body or don't address the actual problem, please read this simple summary of what you need to know: This is not a "Why won't my default installation of…
Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
198
votes
10 answers

How do you run a SQL Server query from PowerShell?

Is there a way to execute an arbitrary query on a SQL Server using Powershell on my local machine?
Tigger78
198
votes
3 answers

Boolean literals in PowerShell

What are the Boolean literals in PowerShell?
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
196
votes
43 answers

Message "the term 'ng' is not recognized as the name of a cmdlet"

Today, while working through some basic AngularJS introduction, I ran into a problem. I opened PowerShell to get going on the project. NPM worked. I was able to install Angular using: npm install -g @angular/cli Anytime I tried to run ng, I would…
Wauna
  • 2,226
  • 2
  • 10
  • 11
195
votes
32 answers

How to get all groups that a user is a member of?

PowerShell's Get-ADGroupMember cmdlet returns members of a specific group. Is there a cmdlet or property to get all the groups that a particular user is a member of?
Primoz
  • 4,079
  • 17
  • 56
  • 67
194
votes
8 answers

How to make PowerShell tab completion work like Bash

Let's say I have the following files in my current directory: buildBar.bat buildFoo.bat buildHouse.bat And I type the following at my command prompt, ./bu and then TAB. In Bash, it gets expanded to ./build In PowerShell, it gets expanded to…
RobSiklos
  • 8,348
  • 5
  • 47
  • 77
193
votes
10 answers

How can I force Powershell to return an array when a call only returns one object?

I'm using Powershell to set up IIS bindings on a web server, and having a problem with the following code: $serverIps = gwmi Win32_NetworkAdapterConfiguration | Where { $_.IPAddress } | Select -Expand IPAddress | Where { $_ -like…
Dylan Beattie
  • 53,688
  • 35
  • 128
  • 197
192
votes
10 answers

Display all environment variables from a running PowerShell script

I need to display all configured environment variables in a PowerShell script at runtime. Normally when displaying environment variables I can just use one of the following at the shell (among other techniques, but these are simple): gci env:* ls…
codewario
  • 19,553
  • 20
  • 90
  • 159
191
votes
2 answers

Executing a command stored in a variable from PowerShell

I have a command that I have build and stored in a variable in PowerShell. This command works if I do a Write-Host and copy and paste into a standard cmd.exe window. How do I execute this command from inside my script? I have tried several…
Travis
  • 3,073
  • 6
  • 28
  • 22
188
votes
9 answers

Use Invoke-WebRequest with a username and password for basic authentication on the GitHub API

With cURL, we can pass a username with an HTTP web request as follows: $ curl -u https://api.github.com/user The -u flag accepts a username for authentication, and then cURL will request the password. The cURL example is for Basic…
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467