0
$testing = @("a.txt", "b.txt")
$testing | Select-String???

I would like the output to only contain a, b. My real example is:

  $PackageNames = Get-ChildItem ("$DestinationPath\$ProjectName" + "_2105\" + $ProjectName) -Filter *.dtsx | Select-Object name -ExpandProperty name;

I need to use the content in this variable but without the extensions.

2
  • 1
    Are you really trying to remove file extensions? There's FileInfo class that has an attribute BaseName, which contains the file name without exension. If that's not your use case, edit the question and explain what kind of patterns you'd like to remove and keep. Can there be input such as ab.txt? Or a.doc? How should those be processed? Commented Sep 29, 2020 at 9:36
  • @vonPryz: Question edited. Commented Sep 29, 2020 at 9:46

2 Answers 2

1

Working with files is easier, if you use FileInfo class' BaseName property. Like so,

$PackageNames = Get-ChildItem ("$DestinationPath\$ProjectName" + "_2105\" + $ProjectName) -Filter *.dtsx

# Process the results
foreach($pkg in $PackageNames) {
  $pkg.BaseName # Just prints the file name without extension
}
Sign up to request clarification or add additional context in comments.

2 Comments

Downvoted by mistake, so I should not use expandproperty?
@David Do not use ExpandProperty in this case. It converts object properties to strings. After such conversion the file object properties are lost, so you can't use, say, .FullName or .DirectoryName properties.
1

Try this:

$a|%{($_.tostring().split("."))[0]}

Iterate over the array, split by a dot and and get the first item.

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.