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

5788 questions
502
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 …
105
votes
4 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" }
93
votes
3 answers

Disable PowerShell beep on backspace

This feature seems to have been added in PowerShell version 5.1, as I don't find it on my laptop still running 5.0. When pressing Backspace in PowerShell, it will emit a 'beep' sound if there is no (more) text to delete. This is quite annoying if…
FastEthernet
  • 5,017
84
votes
9 answers

How to copy text from PowerShell

Sometimes I want to copy a command from Powershell to paste in a document, or I want to copy the output? How can I select and copy text in Powershell? At least I know a way how to paste a text (or a command) into Powershell: you just right-click on…
72
votes
1 answer

I ran a PowerShell script. Was I hacked?

I was an idiot and was suckered into running a PowerShell script. What are the details about it? I have a feeling they only uploaded some files from my system. But I am worried there may be a backdoor left running on my system. The upload I can live…
Moon
  • 763
70
votes
4 answers

Detect if PowerShell is running as administrator

How can I tell in my scripts if PowerShell is running with administrator privileges? I need to know because I'm trying to run a program that requires the ability to open protected ports.
Boomerang
  • 861
62
votes
8 answers

Nano alternative for windows powershell

I am looking for software similar to nano for linux bash but for windows powershell. Is there any built in so I do not have to install something? EDIT Nano is a text editor that runs within the bash. You can open a text like document (.txt, .c etc)…
59
votes
5 answers

Changing last modified date or time via PowerShell

Is it possible to change a file or folders last modified date/time via PowerShell? I have a folder folder1/ and I want to change the last modified date and time of that folder and it's contents via PowerShell.
Jack
  • 831
56
votes
5 answers

How do I permanently remove a default Powershell alias?

For some unknown reason, the devs included "curl" as an alias for Invoke-WebRequest, even though it is not compatible with the proper implementation of curl, which means it gets in the way when I try to make a curl request. I can remove the alias…
48
votes
6 answers

How do I get get-childitem to filter on multiple file types?

I'm trying to get a list of all the XSL and XSLT files in a directory. dir -recurse -filter *.xsl,*.xslt -name But the following error: Get-ChildItem : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Filter'.…
user9141
47
votes
3 answers

Replacements for ampersand and double-ampersand in PowerShell

In Windows' native CMD processor, you can use & to string commands together so that one runs immediately after the other. A && will run the second command only if the first completes without error. This is different from piping commands together…
Iszi
  • 13,775
43
votes
5 answers

Run Powershell script when you open Powershell

Is it possible to run a Powershell script when you run Powershell? As in, double click the Powershell icon and open the window. Is there some type of "auto-run" setting somewhere?
user9993
  • 607
42
votes
3 answers

Powershell Copy-Item recursively but don't include folder name

This is a stupid question, but I just don't know why it isn't working. I am trying to copy the files from FolderA to FolderB recursively. I am doing this: Copy-Item -Path "C:\FolderA\" -Destination "C:\FolderB\" -recurse -Force -Verbose It works…
38
votes
2 answers

Can't exclude a directory pattern recursively

I'm trying to get child items of a folder recursively. However the folder contains noise files and folder (actually, this is a Visual Studio project folder). Here what I have: $root = Get-Item C:\Projects\MyProject $allItems = Get-ChildItem $root…
Steve B
  • 2,222
31
votes
3 answers

How can I find the size of a folder using Powershell?

I want to be able to see how big a folder is (all contents, including sub-folders and their contents). I can't find a powershell command to do that, but I don't want to have to open the windows explorer every time I want to know the size. Is there…
1
2 3
48 49