get-command is the PowerShell equivalent of the Unix which command.
get-command can display the location and information of not only executables, but also aliases, functions, cmdlets and scripts. Basically, anything you can run at the prompt.
get-command is the PowerShell equivalent of the Unix which command.
get-command can display the location and information of not only executables, but also aliases, functions, cmdlets and scripts. Basically, anything you can run at the prompt.
How do you find out the version of PowerShell you are using? This information can be found from the PSVersionTable variable. It holds the versions of the various components of PowerShell.
$ $PSVersionTable
Name Value
---- -----
CLRVersion 2.0.50727.5448
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
To find just the version of the shell component:
$ $PSVersionTable.PSVersion Major Minor Build Revision ----- ----- ----- -------- 2 0 -1 -1
So, I am using version 2.0
Tried with: PowerShell 2.0
The biggest selling point of PowerShell is that it deals with objects instead of mere strings. When putting the pieces of a command pipeline together, one wonders what is the type of objects coming down the pipeline. Finding this out is easy: pipe the output objects to a ForEach-Object loop and print the type of each object.
For example, the type of objects from a text file:
$ Get-Content Foo.txt | ForEach-Object { $_.GetType().FullName }
System.String
System.String
...
Another example, to find the type of objects from a string find:
$ Get-Content Foo.txt | Select-String "blah" | ForEach-Object { $_.GetType().FullName }
Microsoft.PowerShell.Commands.MatchInfo
Microsoft.PowerShell.Commands.MatchInfo
...
Tried with: PowerShell 2.0
If you want to pipe some content to a file, which of these two cmdlets would you use: Out-File or Set-Content?
Out-File is just the same as the output redirection operator (>) you use in any other shell. It formats the output as if it were writing to the console and instead of writing to the console, it writes to the file you specify.
This behaviour of Out-File can be sometimes problematic. If the content you are piping through Out-File has lines of text that are longer than the console width, Out-File will break them at the console width into a newline. So, you might end up with files where the long input lines have been broken down into multiple smaller lines of length equal to the console width.
If you do not want such meddling in your content, stick to using Set-Content to pipe output to files. Set-Content does not mess with the content and writes the textual content pristinely to file.
Tried with: PowerShell 2.0
The output redirection operators > and >> work in PowerShell just like they do in any other shell. > overwrites a file while >> appends to the file.
These output redirection operators are just aliases for the Out-File cmdlet. The equivalent invocations for > and >> are Out-File and Out-File -Append
Tried with: PowerShell 2.0
In the Windows command prompt, environment variables could be viewed by using the set command. In PowerShell, all the environment variables are available on a PowerShell drive named Env:
To list all the environment variables and their values:
$ Get-ChildItem Env: $ dir Env:
To display the value of a specific environment variable, for example the PATH environment variable:
$ $Env:PATH
Tried with: PowerShell 2.0
Tried with: PowerShell 2.0
PowerShell opens up in a soothing white-on-blue console by default. But, the prompt is displayed in the same color as the rest of the text. The color of the prompt can be changed by overriding the default Prompt method by writing a custom Prompt method and placing it in your profile script.
This is a simple Prompt method that sets the color of the prompt to yellow:
function Prompt
{
$promptString = "PS " + $(Get-Location) + ">"
Write-Host $promptString -NoNewline -ForegroundColor Yellow
return " "
}
The above Prompt method is enough if the profile script is meant for the default PowerShell console only. If you are using a profile script that will be used by other PowerShell hosts (like PowerShell ISE for example), then maybe you do not want the prompt to be yellow in those hosts.
For such cases, this is a Prompt method that sets the color of the prompt to yellow only for the default PowerShell console and uses the default color for the other hosts:
function Prompt
{
$promptString = "PS " + $(Get-Location) + ">"
# Custom color for Windows console
if ( $Host.Name -eq "ConsoleHost" )
{
Write-Host $promptString -NoNewline -ForegroundColor Yellow
}
# Default color for the rest
else
{
Write-Host $promptString -NoNewline
}
return " "
}
Tried with: PowerShell 2.0
I keep a PowerShell console open at all times. I find it to be an excellent replacement for the Windows Calculator for quick calculations. Arithmetic operators are part of the PowerShell language, so they can be used directly at the prompt. For mathematical operations like square root, logarithm and those from trigonometry, the System.Math class from the .Net Framework can be used.
For example, to find the square root we can call the Sqrt() method from the System.Math class:
[System.Math]::Sqrt( 20 )
An easier way to do the same is:
[math]::sqrt( 20 )
This shorter case-insensitive version works because PowerShell is designed for an easy console experience. So, in PowerShell all the System.* classes can be invoked without the System prefix. Also, PowerShell tries to be as case-insensitive as possible, so most class and method names can be called ignoring the case.
Tried with: PowerShell 2
Reference: PowerShell in Action (2nd Edition) by Bruce Payette
The Windows command prompt (cmd.exe) has simple command history. You can press the Up/Down arrow keys to cycle through the previous commands. If you want a visual display of previous commands, you can press F7 and use the Up/Down keys to navigate the list and Enter to execute a command.
PowerShell has powerful command history features. To see the list of all available commands from history:
PS> Get-History
To cycle through previous commands from history, press # followed by Tab key presses. Each press of the Tab key, displays one older command from the history stack.
PS> #[Tab]
To switch to a specific command from the stack displayed by Get-History, press #, the number of that command in the stack and Tab. For example, to get the command numbered 9 in the Get-History stack:
PS> #9[Tab]
PowerShell can also find a previous command if you can provide the starting few letters. For example, this will bring up all the commands where I invoked vim:
PS> #vim[Tab]
PowerShell can also understand the * wildcard. For example, this will bring up all the commands where I edited a log file using vim:
PS> #vim log*.txt[Tab] PS> #vim log*[Tab]
The module that will hook you to command history is PowerTab. Install it (for instructions see here) and press # and Tab to get a visual list of all commands in the history stack!
Note that PowerTab does not provide a few of the above features like the wildcards for example.
Tried with: PowerShell 2 and PowerTab 0.99.6