Questions tagged [powershell]

Windows PowerShell is a command line shell and scripting language for Windows that supersedes the CMD.exe shell and batch language. PowerShell is also an automation engine for Windows that provides functionality similar to Windows Script Host and VBScript.

Description of Windows PowerShell

Windows PowerShell is a command line shell and scripting language for Windows that supersedes the CMD.exe shell and batch language. PowerShell is also an automation engine for Windows that provides functionality similar to Windows Script Host and VBScript. The PowerShell engine is hostable allowing PowerShell functionality to be accessed from within custom applications including other PowerShell hosts such as PowerShell ISE, PowerGUI and PowerShellPlus.

PowerShell is built on top of the Microsoft .NET Framework and exposes much of the capabilities of the .NET Framework through the PowerShell scripting language. And since .NET allows for interop with COM, you can also script COM objects.

PowerShell 2.0 is integrated with Windows 7 and Windows Server 2008 R2, and is available for Windows XP SP3, Windows Server 2003 SP2 and Windows Vista SP1 and SP2. Powershell 5.0 is currently the standard on Windows 10 and Server 2012 R2.

Example Usage

# List all processes using > 100 MB of PagedMemory in descending sort order    
C:\PS> Get-Process | Where {$_.PagedMemorySize -gt 100MB} | Sort -Desc

# PowerShell can also evaluate expressions
C:\PS> "Hello World!"
Hello World!

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 args. 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.

Enabling Scripts

Before you can run custom PowerShell scripts (.ps1 files), you need to enable the execution of unsigned scripts. Otherwise, attempting to run a script will produce an error:

.\test.ps1 : File C:\test.ps1 cannot be loaded because running
scripts is disabled on this system. For more information, see
about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\test.ps1
+ ~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

You can set the script execution policy with the Set-ExecutionPolicy cmdlet:

# Allow scripts to be run by all users. This command requires admin privileges
Set-ExecutionPolicy Unrestricted

# Allow scripts to be run by the current user. This does not require special privileges
Set-ExecutionPolicy Unrestricted -Scope CurrentUser

Resources

6102 questions
593
votes
5 answers

How do I run multiple commands on one line in PowerShell?

In a cmd prompt, you can run two commands on one line like so: ipconfig /release & ipconfig /renew When I run this command in PowerShell, I get: Ampersand not allowed. The `&` operator is reserved for future use Does PowerShell have an operator…
David
  • 9,854
538
votes
15 answers

How to enable execution of PowerShell scripts?

When I try to execute my PowerShell script I get this error: File C:\Common\Scripts\hello.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details. At line:1 char:13 …
360
votes
12 answers

Native alternative to wget in Windows PowerShell?

I know I can download and install the aformentioned library (wget for Windows), but my question is this: In Windows PowerShell, is there a native alternative to wget? I need wget simply to retrieve a file from a given URL with HTTP GET. For…
312
votes
6 answers

Equivalent of cmd's "where" in powershell

I can't seem to find anything about a Powershell equivalent of the where command from cmd. Should I just call it from cmd or is there something more elegant in PS?
sunnyseas
290
votes
14 answers

Equivalent of Linux `touch` to create an empty file with PowerShell

Is there an equivalent of touch in PowerShell? For instance, in Linux I can create a new empty file by invoking: touch filename On Windows this is pretty awkward -- usually I just open a new instance of Notepad and save an empty file. So is there a…
jsalonen
  • 9,241
226
votes
9 answers

PowerShell equivalent of curl

Is there an equivalent of curl in PowerShell? Does it have some similar built-in capability or is there a 3rd party cmdlet?
Borek Bernard
  • 15,019
220
votes
11 answers

PowerShell equivalent to the Unix `which` command?

Does PowerShell have an equivalent to the which command found in most (if not all) Unix shells? There are a number of times I'd like to know the location of something I'm running from the command line. In Unix I just do which , and it tells…
Herms
  • 9,992
155
votes
6 answers

What is Windows' equivalent of the "which" command in Unix? Is there an equivalent PowerShell command?

In Linux, we have the "which" command to find out the path of an executable. What is its Windows equivalent? Is there any PowerShell command for doing that?
Invincible
140
votes
11 answers

Copy and paste in Windows PowerShell

How do I effectively copy and paste input and output in the Windows PowerShell?
orschiro
  • 3,805
136
votes
16 answers

Delete all files from a folder and its sub folders

I want to remove all files from a folder structure, so I'm left with an empty folder structure. Can this be achieved in either batch or VBScript scripting? I have tried a very basic batch command, but this required the user to allow the deletion of…
RobN
  • 1,812
  • 5
  • 19
  • 28
111
votes
10 answers

how to run a powershell script as administrator

On my Windows 7 Desktop, I have script.ps1, which needs admin privileges (it starts a service). I want to click on this script and run it with admin privileges. What's the easiest way to accomplish this?
Sajee
  • 6,869
106
votes
5 answers

have Powershell get-childitem return files only

I'd like to use get-childitem recursively, but only have it return files not directories. The best solution I have just doesn't seem natural: gci . *.* -rec | where { $_.GetType().Name -eq "FileInfo" }
Frank Schwieterman
  • 1,445
  • 3
  • 12
  • 20
104
votes
18 answers

Linux "Top" command for Windows Powershell?

I am looking for a PowerShell cmdlet that can provide similar functionality to the Linux Top app. Something that refreshes at some given interval and displays the process list with CPU % util. I have seen scripts that list CPU % utilization in a…
TimAtVenturality
104
votes
9 answers

Turning off the cmd window beep sound

Is there a way to essentially mute the Beeping function of the Windows command shell? I'm working on a PowerShell script right now which ends up printing several lines of text to the screen. I'm working out a bug in the encoding logic. But every…
JaredPar
  • 1,331
98
votes
2 answers

run powershell command from cmd

how i can run this command from cmd : powershell.exe "(get-process | ? {$_.Description -eq "Sysinter Process Explorer"}) | select processname | out-file $env:APPDATA\example.txt" i still get this error : You must provide a value expression on…
1
2 3
99 100