Summary: This is a set of frequently asked/answered question list. This faq is a wiki, and as such is user developed, and unofficial.
The home page for the Channel9 Windows
PowerShell Wiki is
WindowsPowerShellWiki
1. How do I get Help?
From the shell, type get-command get-help
2. I mean, I don't understand the syntax of xxxxx!
Look at the docs and the Quick Reference (WindowsPowerShellQuickStart)!
First make friends with get-command, get-help, and get-member. If you want to find out how to use a cmdlet and already know the name, get-command and get-help are your friends. Usign get-command returns the full name or the path of the actual command source - script, alias expansion, cmdlet, or function - and if it is immediately resolved unambiguously you will get a syntax synopsis - e.g., to get a synopsis for the get-comand cmmand, do this:
PS>get-command get-command
You can get the complete internal help using either get-help or the -? ubiquitous parameter. Both of the following are equivalent:
PS>get-help get-command
PS>get-command -?
If you know how to use a command but don't know what to do with the results from it, get-member is your friend, too. Just run the command and pipe its output directly into get-member. Use the parameters ""-typeinfo"", ""-property"", or ""-method"" to show particular members of the data. For example:
PS>get-childitem | get-member -typeinfo
Class Name
MemberData ----- ---- ----------
Type
FileInfo System.IO.FileInfo Type
DirectoryInfo System.IO.DirectoryInfo
3. How do I use history/completion/etc?
ToDo - Need to describe the Function keys used.
4. Where do all these aliases come from?
Most aliases are defined in profile.ps1, although some (such as gch and sas) are hard-coded into powershell.exe.
5. How do I create an alias for a command?
Here's a simple simple demo of using set-alias.
set-alias sal set-alias #makes sal like the legacy alias command
set-alias gal get-alias #makes gal list aliases
set-alias gl get-location #gl corresponds to Win2K+ %CD%
set-alias sl set-location #sl corresponds to cd
6. What is the command for XXXX?
This should show a table of legacy DOS commands with corresponding cmdlets, showing each of the forms - DOS version, full cmdlet name for cmdlets that provide similar functionality, and then the 'suggested' abbreviation.
7. Why does what I see look funny?
This will come up forever just because it is not something that can be solved to everyone's liking. There's a pithy principal in here somewhere, something like 'What You See Represents What You Get'.
As an example, suppose you're looking at some text files in the current folder. Windows
PowerShell has a get-childitem cmdlet, and if we just issue the command
PS>get-childitem *.txt
we see a listing that looks kind of like this:
Mode [LastWriteTime] Length Name
---- ------------- ------ ----
-a--- Aug 26 18:16 2,078 about_Alias.help.txt
-a--- Aug 26 18:16 3,045 about_Arithmetic_operators.help.txt
-a--- Aug 26 18:16 1,890 about_Array.help.txt
This listing may not look odd to you, but what if you're wanting to get the fully qualified paths for these files? It isn't shown!
In fact, it doesn't matter. Windows
PowerShell has returned object representations of all of those files, and the path is returned with them as well as the things you see in the listing. What you're seeing is some the things that the Windows
PowerShell developers decided generally describe files to most people. It may not apply to YOUR needs, but that's OK because the rest of the information is still there.
To see what attributes there are, you can run them through the get-member command:
PS>get-childitem *.txt | get-member -property
This will return a large number of property names, including the following:
Class Name [MemberData]
----- ---- ----------
Property [CreationTime] [System.DateTime]
Property [DirectoryName] System.String
Property Exists System.Boolean
Property Extension System.String
Property [FullName] System.String
Property [ParentPath] System.String
Property Path System.String
You can then use the pick-object command to extract just a property and either use that further or just let it display. In general terms you want the Path property, but for a traditional file-like name you would just use
FullName, like this:
PS>get-childitem *.txt | pick-object [FullName]
[FullName]
--------
C:\Program Files\Windows command shell preview\about_Alias.help.txt
C:\Program Files\Windows command shell preview\about_Arithmetic_operators.help.txt
You can think of the screen-displayed text in Windows
PowerShell like a restaurant menu. A menu is a text description of something that you can eat. It is hopefully descriptive and useful as a guide, but the words are not the thing itself, and don't give the whole picture. It doesn't matter; when you order the item, you get the real thing, and if it's made with fresh cilantro, you get fresh cilantro, even though the word cilantro doesn't appear in the description. If you don't know whether it has cilantro, you can always ask.
The same with Windows
PowerShell. A file has all the filish attributes even though you don't see them, and if you don't know if it comes with something you need, just use get-member to 'ask' what properties and methods a file has. -- aka
8. Why doesn't Windows PowerShell understand my changes to xxxx?
If this involves cmdlets or help, restarting Windows
PowerShell at this point is good.
9. How do I change my prompt?
By defining the function prompt!
An example of this is as follows:
function prompt
{
# make sure the end of the path is always visible at the prompt
$local:path = [$(get-location).ToString()]
$local:maxPath = [int] [$PSHost.UI.RawUI.WindowSize.Width] - 1
if ($local:path.length -gt $local:maxPath)
{
$local:x = [int] [System.Math].Max(0, $([int] $local:maxPath - 10))
$local:path = '...' + $local:path.Substring(($local:path.length - $local:x), $local:x )
}
$local:counter = $(get-history -count 1).Id
$local:counter += 1
# also show the path at the command windows title bar
[$PShost.UI.RawUI.WindowTitle=$('PS] - ' + $path)
#Show the prompt in blue
write-host $('PS {0:d} {1}' -f $local:counter, $local:path) -fore Blue
'>'
}
10. What does this variable xxx mean?
Windows
PowerShell has a number of specialised variables as follows:
$
@
&
$Args - an array of paraneters passed to a function
$input - the collection of all objects I'm working on
$_ - the object I'm currently working on.
$$ - the last token of the last line reveived by the shell
$^ - the first token of the last line reveived by the shell
$? - the success/fail status of the last operation
{ } -
${ } -
( ) -
$( ) -
@( ) -
&( ) -
&{ } -
11. A lot of the parameters have long names. How do I avoid typing so much?
Even though best practice when
showing people how to use Windows
PowerShell is to show the full name of a parameter, you don't need to type the entire name when working interactively. In fact, that would make the shell discouraging to use.
The way you get around this is with 2 techniques.
First, alias the commands you use frequently. I use the standard 'suggested' abbreviations, which means I have aliases like the following:
set-alias gcm get-command
set-alias gm get-member
Second, when typing parameters, you ONLY need to type enough of the parameter name to differentiate it from any other parameter for the command.
For example, let's look at the definition of get-member:
PS>get-command get-member | pick-object Definition
Definition
----------
get-member
-InputObject PSObject -Property -TypeInfo -Method -Set The ""-Property"" parameter is the only one beginning with p, so if you simply use ""-p"" Windows
PowerShell knows you mean ""-Property"". You can specify that you want to see properties, type, methods, and you want subsets expanded by just using the sequence ""-p -t -m -s"".
Let's say you have aliased gm to point to get-member as well; then a command line like this:
PS>$foo | get-member -Property
-TypeInfo -Method -Set
can be typed like this:
PS>$foo | gm -p -t -m -s
12. Why can I do "c:", but not "hklm:" or "cert:" out of the box with Windows PowerShell?
The 'x:' way to set a drive is really a set of functions dynamically
created by Windows
PowerShell from a chunk of code in your profile.ps1 file, located in
My Documents\PS. The bit of code that does it is this:
# This function will define 26 functions with names of the form X:
where X # is a letter [A-Z]. The functions set the CWD to the drive of
the same # name.
& {
for ($i = 0; $i -lt 26; $i++)
{
$funcname = ([System.Char]($i+65)) + ':'
$str = "function global:$funcname { set-location $funcname } "
invoke-command $str
}
}
So what is really does is create a set of functions that look like this:
function global:c:{ set-location c:}
Note that the functions are
created even if you don't have a physical drive there, so behavior is
very much like cmd.
Below is a variation that you can use. You just need to insert it into your profile.ps1
file:
foreach ($drive in get-drive){
$name = $Drive.Name + ':';
invoke-command "function global:$name {set-location $name}"
}
This script creates set-location functions for drives that exist when Windows
PowerShellstarts up, of course, but it works ok and you can do things like
cert:
hklm:
alias:
function:
13, Why can't I tab complete cmdlets?
This would certainly be be convenient, and many developers are likelty to feel the same way. The principal reason why this has not been done for tab-completion of commands is purely due to feature prioritization. Its definitely something MS want to get to but most likely it won't be for V1.
For those of you that are implementing hosts, it would be quite trivial to implement tab-completion for commands. Just call "get-command".
PS> set-l<tab>
would end up being a call like "get-command set-l*". Just keep a buffer of the returned results and cycle through them as the user hits tab. The hardest part of tab completion is rewriting the UI as the user hits tab multiple times (but its not that hard).
14. My output gets newlines inserted at 80 characters, and I don't want that! How do I make it stop?
Instead of redirecting output to a file, use out-file -width 200 (or whatever width is sufficient to relieve your pain).
15. I can't use parameters on my alias! What's that all about?
One of the limitations of the current drop is the inability to add parameters to an alias:
This will not work: PS> set-alias look {gch *.dll -recurse}
My way around this is to create functions for use as aliases:
PS>function global:versions{gps | pick fileversion, processname | out-grid fileversion, processname}
Now you just have to type: PS> versions
to get the command.
16. How do I enter multiple lines of script in a interactive session?
Use the grave-accent(`) at the end of a line to enter into multi-line mode (signified with a >> prompt). This allows you to split your script accross many lines without executing it. To execute the script simply press return at a blank >> prompt. If the interpreter detects that your script is not syntactically "closed" i.e. it is expecting a closing brace or bracket, it will continue to prompt for more script irrespective of how many times you press return. If you decide you dont want to continue multi-line mode press control-c to break back into normal mode.
17. What are the escape sequences for characters (e.g. newline )?
Use the grave-accent(`) within a quoted string to indicate the next character is a special character e.g
"This is a sentence`ncontaining a new line!"
will display the following text at the console:
This is a sentence
containing a newline!
To find out more escape sequences run
help about_escape_character
Simple, but extremely useful for some...
META FAQs
1. How do I pronouce cmdlet?
Command-let