2

I can do something like

ls . `
| select @{ Name="Dir"; Expression = { $_ | Split-Path } } `
| select -ExpandProperty Dir

to select a custom expression (in this case $_ | Split-Path) into a simple array of values.

Is there a way to merge the two select statements into one, that still yields equivalent results?

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • 2
    `(ls | select @{ Name="Dir"; Expression = { $_ | Split-Path } }).dir` ? – arco444 Oct 30 '17 at 08:48
  • @arco444: That's better, but it still requires me to choose a name and construct the entire object. Is there no way to just give the expression `{ $_ | Split-Path }` and a flag or something that indicates I want the values as a plain array? – Tomas Aschan Oct 30 '17 at 09:41
  • 1
    `it still requires me to choose a name and construct the entire object` you should only do this if you actually need an object. If you're just looking to transform a single property and capture the output, use a `foreach` – arco444 Oct 30 '17 at 10:21
  • i.e. from your question: `ls | % { $_ | Split-Path }`. This method would work given the comment you left on the answer. Perhaps you should add this information in the question. – arco444 Oct 30 '17 at 10:24
  • Do you mean `Get-ChildItem | Select-Object -ExpandProperty DirectoryName`? – Bill_Stewart Oct 30 '17 at 18:21

2 Answers2

1

I think the OP is looking for ForEach-Object (MSDN), which performs an operation against each item in a collection of input objects. It's similar to the map or Select operation in other languages.

So instead of:

ls . `
| select @{ Name="Dir"; Expression = { $_ | Split-Path } } `
| select -ExpandProperty Dir

You want:

ls . `
| ForEach-Object { $_ | Split-Path }

You can also replace ForEach-Object with %. But it's less readable and less searchable for new PowerShell users:

ls . `
| % { $_ | Split-Path }
Chris Xue
  • 2,317
  • 1
  • 25
  • 21
0

What are you trying to do? You could get the same results with just ls/Get-ChildItem and Split-Path. Not sure if you need the Microsoft.PowerShell.Core\FileSystem:: part.

PS C:\Path\To\Dir> Get-ChildItem | Split-Path
Microsoft.PowerShell.Core\FileSystem::C:\Path\To\Dir
Microsoft.PowerShell.Core\FileSystem::C:\Path\To\Dir
Microsoft.PowerShell.Core\FileSystem::C:\Path\To\Dir

PS C:\Path\To\Dir> (Get-ChildItem).FullName | Split-Path
C:\Path\To\Dir
C:\Path\To\Dir
C:\Path\To\Dir

Edit

Assuming nuget list <my-package> outputs a string array, you can use -replace and regex ($ to match string end) to manipulate it and get an array of results.

$((nuget list <my-package>).Replace(" ",",") -replace "$",".nupkg")
G42
  • 9,791
  • 2
  • 19
  • 34
  • In reality, I do this against the result of `nuget list `, and the expression is actually `{ "$($_.Replace(" ", ",")).nupkg" }` but I wanted an example that could run on any computer, not just mine, so your approach isn't really applicable. – Tomas Aschan Oct 30 '17 at 09:58
  • 1
    @TomasLycken OK, that's thoroughly confusing. Updated the answer with what I believe you're after. Why not post the actual expression, current output and desired output? Combining `Selects` is not the way to do it AFAICS. In your example, your expression is the function `Split-Path` which accepts pipeline input and your dealing with `Get-ChildItem` which has properties you can leverage. In your actual case, you're using the `.Replace` method and I'm assuming an array. – G42 Oct 30 '17 at 11:19