# PowerShell One-Liners: Help, Syntax, Display and Files

PowerShell is designed to be used by busy IT professionals who want to get things done, and don't necessarily enjoy programming. PowerShell tackles this paradox by providing its own help and command-line intellisense. We aim to make it a bit easier still by providing a series of collections of general-purpose one-liners to cover most of what you'll need to get useful scripting done.

This series is in four parts: This is part 1

Well, yes, it would be an onerous task indeed to deliver everything. But per the Pareto principle, roughly  eighty percent of what you could want to know about PowerShell is here in this series of articles; possibly more!  You will tend to learn the other twenty percent  as you go, and you may not need  it for quite a ways into your PowerShell explorations.  I feel compelled to confess that some of the entries are not, strictly speaking, on one physical line, but  are written in such a way that, if you really want to run them in one line, you can just remove the line breaks and they will work. (Note that that is not generally true of PowerShell syntax.)

This series of articles evolved out of my own notes on PowerShell as I poked and prodded it to show me more. As my collection  burgeoned, I began to organize them until I had one-line recipes for most any simple PowerShell task. Simple, though, does not mean trivial. You can do quite a lot in one line of PowerShell, such as grabbing the contents of specific elements of a web page or converting a CSV file into a collection of PowerShell objects.

This collection of PowerShell one-liners is organized as follows:

Part 1
begins by showing you how to have PowerShell itself help you figure out what you need to do to accomplish a task, covering the help system as well as its handy command-line intellisense. The next sections deal with locations, files, and paths: the basic currency of any shell. You are introduced to some basic but key syntactic constructs and then ways to cast your output in list, table, grid, or chart form.
Part 2
moves into details on variables, parameters, properties, and objects, providing insight into the richness of the PowerShell programming language. Part 2 is rounded out with a few other vital bits on leveraging the PowerShell environment.
Part 3
covers the two fundamental data structures of PowerShell: the collection (array) and the hash table (dictionary), examining everything from creating, accessing, iterating, ordering, and selecting. Part 3 also covers converting between strings and arrays, and rounds out with techniques for searching, most commonly applicable to files (searching both directory structures as well as file contents).
Part 4
is your information source for a variety of input and output techniques: reading and writing files; writing the various output streams; file housekeeping operations; and various techniques related to CSV, JSON, database, network, and XML.

Each part of this series is available as both an online reference here at Simple-Talk.com, in a wide-form as well, and as a downloadable wallchart (from the link at the bottom of the article) in PDF format for those who prefer a printed copy near at hand. Please keep in mind though that this is a quick reference, not a tutorial. So while there are a few brief introductory remarks for each section, there is very little explanation for any given incantation. But do not let that scare you off-jump in and try things! You should find more than a few “aha!” moments ahead of you!

## Notes on using the tables:

• A command will typically use full names of cmdlets but the examples will often use aliases for brevity. Example: Get-Help has aliases man and help. This has the side benefit of showing you both long and short names to invoke many commands.
• Most tables contain either 3 or 4 columns: a description of an action; the generic command syntax to perform that action; an example invocation of that command; and optionally an output column showing the result of that example where feasible.
• For clarity, embedded newlines (n) and embedded return/newline combinations (rn) are highlighted as shown.
• Many actions in PowerShell can be performed in more than one way. The goal here is to show just the simplest which may mean displaying more than one command if they are about equally straightforward. In such cases the different commands are numbered with square brackets (e.g. “[1]”). Multiple commands generally mean multiple examples, which are similarly numbered.
• Most commands will work with PowerShell version 2 and above, though some require at least version 3. So if you are still running v2 and encounter an issue that is likely your culprit.
• The vast majority of commands are built-in, i.e. supplied by Microsoft. There are a few sprinkled about that require loading an additional module or script, but their usefulness makes them worth including in this compendium. These “add-ins” will be demarcated with angle brackets, e.g. <<pscx>> denotes the popular PowerShell Community Extensions (http://pscx.codeplex.com/).
• There are many links included for further reading; these are active hyperlinks that you may select if you are working online, but the URLs themselves are also explicitly provided (as in the previous bullet) in case you have a paper copy.

Note: Out of necessity, the version of the tables in the articles is somewhat compressed. If you find them hard to read, then there is a wide version of the article available here, and a PDF version is available fron the link at the bottom of the article

## What’s What and What’s Where

This is your starting point when you are staring at a PowerShell prompt, knowing not what to do. Find out what commands are available, read help on PowerShell concepts, learn about auto-completion of cmdlets and parameters, see what command an alias refers to, and more.  Before even going to the first entry, though, it is useful to learn one thing: PowerShell has help available on both commands and concepts. You can look up a command simply with, for example, Get-Help Get-ChildItem (# 1 below). Or you can search for a command with a substring, e.g. Get-Help file (#3). But as with any programming language you have to know syntax, semantics, structures, … in short all those conceptual items that let you work with commands. If you want to know about variables, for example, you have merely to say Get-Help about_variables. All conceptual topics begin with the “about_” prefix (#11).

Not at first, but in short order, you will want to be able to find meta-details about commands as well. By that, I mean to answer questions like: What type of objects does a cmdlet return? Does a cmdlet have one or multiple sets of parameters available?  Where does a cmdlet come from? All of those meta-details can be seen from entry 20 below.

 Action Command Example Basic help for x Get-Help cmd (cmd is a full name) help Get-ChildItem Help in separate window Show-Command cmd; then select the help icon Show-Command Get-ChildItem List help topics containing x Get-Help string (string is a prefix or uses wildcards) [1a] help Get[1b] help Get-* Help for parameter y of cmdlet x Get-Help cmd -parameter y help Get-Date -param month Help for multiple parameters Get-Help cmd -parameter y* (i.e. use wildcards) help Get-Date -param m* List allowed values for parameter y of cmdlet x Get-Help cmd -parameter y help Out-File -parameter Encoding Intellisense for parameter names [in ISE] cmdlet -paramNamePrefix Out-File -enc Intellisense for parameter values [in ISE] cmdlet -paramName paramValuePrefix Out-File -enc Auto-completion for parameter names cmdlet – paramNamePrefix [1a] Out-File -[1b] Out-File -enc Auto-completion for parameter values cmdlet -paramName paramValuePrefix [1a] Out-File -enc[1b] Out-File -encu List all ‘conceptual’ topics (see text above) Get-Help about_* same Filter help output by regex Get-Help topic | Out-String -stream | sls -pattern regex help ls | Out-String -Stream | Select-String recurse Filter help output by constant Get-Help topic | Out-String -stream | sls -simple text help ls | Out-String -Stream | sls -SimpleMatch “[-recurse]” Send help text to a file [1] Get-Help topic| Out-String | Set-Content file[2] Get-Help topic > file [1] help Get-ChildItem | Out-String | sc help.txt[2] help Get-ChildItem > help.txt List all cmdlets and functions Get-Command same List all cmdlets/functions beginning with characters Get-Command string* gcm wr* List all cmdlets/functions filtered by noun Get-Command -noun string* gcm -noun type* List all exported items from module x Get-Command -Module module Get-Command -Module BitLocker List properties and methods of cmdlet cmdlet | Get-Member Get-ChildItem | gm List meta-details of cmdlet (see text above) Get-Command cmdlet | Select-Object * gcm Get-ChildItem | select * Display module containing cmdlet (Get-Command cmdlet).ModuleName (gcm Get-ChildItem).ModuleName Display assembly containing cmdlet (for compiled cmdlets) ( Get-Command cmdlet ).dll (gcm Get-ChildItem).dll Display underlying command for alias x Get-Alias -name x Get-Alias -name gci Display aliases for command x Get-Alias -definition x Get-Alias -def Get-ChildItem Get general help for PS Community Extensions Import-Module pscx; Get-Help pscx <> same List all functions in PSCX Get-Command -Module Pscx* -CommandType Function <> same

## Location, Location, Location

See where you are or where you have been and navigate to where you want to be; understand the difference between your PowerShell current location and your Windows working directory; get relative or absolute paths for files or directories.

## Basic Syntactic Elements

Like with learning most things, you need to learn to crawl before you can learn to run. This section shows you how to do some of the most basic but important things, things that will soon become second nature: knowing what is true and what is false; adding comments; continuing a command across multiple lines or, contrariwise, combining multiple commands on one line; and so forth. Arguably there should be one other important group of items included here: PowerShell operators. But I already published a wallchart on a set of common operators for strings and string arrays. See Harnessing PowerShell’s String Comparison and List-Filtering Features (http://bit.ly/1c20itX) for details on -eq, -like, -match, and -contains operators and their variations.

## Conclusion

That’s it for part 1; keep an eye out for more in the near future! While I have been over the recipes presented numerous times to weed out errors and inaccuracies, I think I may have missed one. If you locate it, please share your findings in the comments below!