2

I want to shorten Directory with relative path:

$Dir = get-childitem C:\temp -recurse
$List = $Dir | where {$_.extension -eq ".txt"}
$List | format-table name, Directory -replace "C:\temp", ""

I get this error:

Format-Table : A parameter cannot be found that matches parameter name 'replace'.
At line:3 char:38
+ $List | format-table name, Directory -replace "C:\temp", ""
+                                      ~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Format-Table], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.FormatTableCommand

What's the right syntax ?

0

3 Answers 3

4

You can use a calculated property. Example:

$List | Format-Table name,
  @{Name = "Directory"; $Expression = {$_.FullName -replace "C:\\temp", ""}}

A calculated property is simply a hashtable that dictates the content of the property. Calculated properties are available with formatting cmdlets that select properties and output a new custom object (e.g, Select-Object, Format-List, etc.).

(As an aside: The -replace operator uses a regular expression, so you would need to write C:\\temp instead of just C:\temp.)

If your goal is to output file system item directory names: Directory is not a property of all file system objects. Is this what you mean?

Get-ChildItem C:\Temp\*.txt -Recurse | Format-Table Name,
  @{Name = "Directory"; Expression = {$_.FullName -replace 'C:\\temp', ''}}

Note how this command takes advantage of the pipeline (no need for the intermediate $List or $Dir variables).

Sign up to request clarification or add additional context in comments.

7 Comments

I believe you need a double slash for the -replace regex to work.
@Bill_Stewart I get this error: A null key is not allowed in a hash literal.
@HAL9256 I added double slash but there seems there's already an error see my comment above
I think it's not "Name" but "Label" see answer below
Ah @Bill_Stewart You are right, I didn't know that it had that alias.
|
2

To add to @Bill_Stewart's Answer.

$Dir = get-childitem C:\temp -recurse
$List = $Dir | where {$_.extension -eq ".txt"}
$List | format-table name, @{Label="Directory"; Expression={$_.Directory -replace "C:\\temp", ""}}

4 Comments

I still get this error: A null key is not allowed in a hash literal.
When you run just $List | format-table name, Directory are there any "bad" Directory entries?
I had some other filters, I recreate from scratch adding filters aftewards now it works :)
I don't know to whom attribute right answer so I checked first one but you've been also helpfull so thanks.
1

I know this is old, but I came across this as I had a similar issue. I didn't see the final solution in here, so I fiddled with it and this is what I got to work. Had to replace double quotes with single quotes in the hash.

$Dir = get-childitem c:\temp -recurse
$List = $Dir | where {$_.extension -eq ".txt"}
$List | format-table name, @{Label='Directory'Expression={$_.Directory -replace 'C:\\temp', ''}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.