1

I'm rather baffled by Powershell in general. Very weird. Anyway, I've read:

Powershell's equivalent to Linux's: ls -al

so I now know how to list the contents of the current directory. But how can I list the contents of an arbitrary directory?

Specifically,

  • How do I type in the path I want to check?

    • in Windows, \ is the directory separator; but it's also an escape char in most languages. What do I do with it?
    • Do I need single-quotes? Double-quotes? No-quotes?
  • Where do I place the argument relative to the switches? After, like I'm used to, or rather before?

  • If I want to use an environment variable, or a powershell variable, as part of the path to list - how do I do that?

einpoklum
  • 102,731
  • 48
  • 279
  • 553

1 Answers1

1

How do I type in the path I want to check?

Get-ChildItem offers two ways to specify one or more input paths, as most file-processing cmdlets in PowerShell do:

  • -Path accepts one or more names or paths that are interpreted as a wildcard pattern.

    • Note that - unlike in POSIX-compatible shells such as Bash - it does not matter whether the path is unquoted or not; e.g., Get-ChildItem -Path *.txt, Get-ChildItem "*.txt", and Get-ChildItem '*.txt' are all equivalent (note the incidental omission of -Path in the latter two calls, which makes "*.txt" and '*.txt' bind positionally to the first positional parameter, -Path).
  • -LiteralPath accepts one or more names or paths that are assumed to refer to existing file-system items literally (verbatim).

    • Binding to -LiteralPath via an argument requires explicit use of -LiteralPath, i.e. use of a named argument; e.g., Get-ChildItem -LiteralPath foo

    • However, supplying System.IO.FileInfo and/or System.IO.DirectoryInfo instances (such as output by (another) Get-ChildItem or Get-Item call) via the pipeline DOES bind to -LiteralPath, as explained in this answer.

Where do I place the argument relative to the switches? After, like I'm used to, or rather before?

Only parameters that require a value (argument) can potentially be passed as values only - depending on the parameter declaration of the target command - in which case their order matters:

  • Value-only arguments are called positional arguments, and they bind in order to those parameters of the target command that are declared as positional, if any - see this answer for how to discover which of a given command's parameters are positional ones.

  • Prefixing a value by its target parameter (e.g., -LiteralPath /some/path) makes it a named argument.

A switch (flag) in PowerShell, such as -Force, is a special parameter type - Boolean in nature - that by definition requires passing it as a named argument, typically without a value (though you can attach a value, e.g. -Force:$true or -Force:$false - note that : is then required to separate the parameter name from its value; see this answer for details).

Since PowerShell allows named arguments to be specified in any order, the implication is that you're free to place by-definition-named switch arguments such as -Force anywhere on the command line.

See the conceptual about_Parameters help topic for more information.

If I want to use an environment variable, or a powershell variable, as part of the path to list - how do I do that?

To use such variables as-is:

# PowerShell variable
Get-ChildItem -LiteralPath $HOME

# Environment variable, e.g. on Windows:
Get-ChildItem -LiteralPath $env:USERPROFILE

To make a variable (or expression) part of a larger string, embed it in a expandable (double-quoted) string ("..."); e.g.:

Get-ChildItem -LiteralPath "$HOME/Desktop"

Note:

  • Embedding the output from expressions or commands requires use of $(...), the subexpression operator; e.g.
    Get-ChildItem "$(Get-Variable -ValueOnly Home)/Desktop"; for a complete overview of PowerShell's expandable strings (string interpolation), see this answer.

  • Situationally, such as in the example above, omitting the "..." quoting works too - see this answer for more information.

Additionally, PowerShell allows you to use (...), the grouping operator to pass the result of arbitrary expressions and commands as arguments; the following is the equivalent of the command above:

Get-ChildItem -LiteralPath ($HOME + '/Desktop')

Alternatively, using Join-Path:

Get-ChildItem -LiteralPath (Join-Path $HOME Desktop)
mklement0
  • 312,089
  • 56
  • 508
  • 622