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
21
votes
4 answers

Displaying a NuGet package's dependencies

When you show the Manage NuGet Packages dialog box, it will show the available packages with a lot more information than is apparently available than from the powershell. In particular, is there a way from the powershell which will list the…
Unsliced
  • 10,404
  • 8
  • 51
  • 81
21
votes
3 answers

Powershell online interpreter?

Is there online Powershell interpreter available on the Internet? I mean like trypython for Python programming language.
jrara
  • 16,239
  • 33
  • 89
  • 120
21
votes
2 answers

Powershell: Colon in commandlet parameters

What's the deal with Powershell commandlet switch parameters that require a colon? Consider Exchange 2010 management shell cmdlet Move-ActiveMailboxDatabase. The Confirm switch is a System.Management.Automation.SwitchParameter and must be used like…
vonPryz
  • 22,996
  • 7
  • 54
  • 65
21
votes
2 answers

Powershell Calling .NET Assembly that uses App.config

I have a Powershell script that is loading a .NET assembly (.EXE in my case) and calling a public method that uses the app.config to pull an encrypted connection string. The script dynamically copies the assembly's exe.config over to the Powershell…
GabeA
  • 343
  • 2
  • 3
  • 8
21
votes
3 answers

How do I output lines that do not match 'this_string' using Get-Content and Select-String in PowerShell?

I found a post about users that wanted to use grep in PowerShell. For example, PS> Get-Content file_to_grep | Select-String "the_thing_to_grep_for" How do I output lines that are NOT this_string?
chrips
  • 4,996
  • 5
  • 24
  • 48
21
votes
1 answer

Convert xargs Bash command to PowerShell?

I've got a simple Bash command to resize some images automatically on a low-traffic website using ImageMagick - I'd like to convert this to a PowerShell command so I don't have to install Cygwin on my webserver. Can anyone lend their PSh skills…
Ana Betts
  • 73,868
  • 16
  • 141
  • 209
21
votes
8 answers

http requests with powershell

I am looking to make http requests to web pages with powershell, is this possible and if so, how may I achieve this? Can I make requests to https pages? I am able to make http requests with a bat file but not https, was hoping I could https page…
amateur
  • 43,371
  • 65
  • 192
  • 320
21
votes
7 answers

Confusing Powershell behavior

Am a bit confused w/ remote executing a powershell command. I have a test server (Win 2k8-R2-SP1) called ServerA, which has powershell remoting enabled correctly. From my dev machine (Win 2k8-R2-SP1), am able to remote execure powershell commands…
Prasanna K Rao
  • 1,086
  • 2
  • 8
  • 18
21
votes
1 answer

Is it possible to have multiple tabs in one PowerShell terminal window on Windows 10?

I need to work with several terminal windows simultaneously, and it's very inconvenient to constantly have to switch between different terminal windows. Is there a way to have multiple tabs in one window? I know it's possible on macOS and Linux, but…
JavaGeek
  • 335
  • 1
  • 2
  • 11
21
votes
1 answer

I can't download linux with wsl, Invalid command line option: --install

wsl --install --online Invalid command line option: --install Copyright (c) Microsoft Corporation. All rights reserved. I need to see the linux versions I can download but I get this output. Why ?
selcuk
  • 272
  • 1
  • 2
  • 7
21
votes
9 answers

How to access the 64-bit registry from a 32-bit Powershell instance?

If you launch a 32-bit instance of Powershell (%SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe), then the registry provider only sees the limited 32-bit parts of the registry. **32-bit console** PS> (dir HKLM:\SOFTWARE | measure).count -…
Richard Berg
  • 20,629
  • 2
  • 66
  • 86
21
votes
6 answers

Running Powershell from .Net Core - Could not load file or assembly Microsoft.Management.Infrastructure

I have been trying to run a powershell script from a .Net Core Web app (not discussing best practices here ;) ) with the following code: string command = @"& """c:\\my Folder\\myScript.ps1"""; using (var ps = PowerShell.Create()) { …
Sylvain Gantois
  • 779
  • 1
  • 12
  • 28
21
votes
2 answers

The term 'conda' is not recognized as the name of a cmdlet

I have installed Anaconda 2019.03 for Windows Installer in Windows 10. When typing anything which starts with conda on Powershell getting error: conda : The term 'conda' is not recognized as the name of a cmdlet, function, script file, or operable…
isifzade
  • 480
  • 1
  • 5
  • 16
21
votes
1 answer

How to declare a variable and its type is Boolean in PowerShell?

When I study PowerShell scripting language, I knew the data type is auto-assignment. If I want to declare a Boolean type variable, how can I do it? Example: $myvariable = "string" or $myvalue = 3.14159 $myboolean ?
HushChen
  • 345
  • 1
  • 2
  • 5
21
votes
6 answers

Install Python with cmd or powershell

My question is if you can install python with powershell, cmd, vbs or any other language built into Windows already? If this was already asked please redirect me to the answer. "How to install Python using Windows Command Prompt" explains how to…
user10122572