farag
farag

Reputation: 375

Remove part of the string in powershell in hashtable

I got driver list

$HashTable = Get-WindowsDriver –Online -All | Where-Object {$_.Driver -like "oem*.inf"} | Select-Object Driver, OriginalFileName, ClassDescription, ProviderName, Date, Version
Write-Host "All installed third-party drivers" -ForegroundColor Yellow
$HashTable | Sort-Object ClassDescription | Format-Table

The table displays full inf file path in a OriginalFileName column. I need to cut full path e.g.

C:\Windows\System32\DriverStore\FileRepository\pantherpointsystem.inf_amd64_bde4cf569a728803\pantherpointsystem.inf

to

pantherpointsystem.inf.

And so that way in all lines.

Upvotes: 0

Views: 261

Answers (3)

Theo
Theo

Reputation: 61068

To split the filename from the complete path, you can use Powershells Split-Path cmdlet like this:

$FullPath = "C:\Windows\System32\DriverStore\FileRepository\pantherpointsystem.inf_amd64_bde4cf569a728803\pantherpointsystem.inf"
$FileName = $FullPath | Split-Path -Leaf

or use .NET like this:

$FullPath = "C:\Windows\System32\DriverStore\FileRepository\pantherpointsystem.inf_amd64_bde4cf569a728803\pantherpointsystem.inf"
$FileName = [System.IO.Path]::GetFileName($FullPath)

In your case, I'd use a calculated property to fill the Hashtable:

$HashTable = Get-WindowsDriver –Online -All | 
                Where-Object {$_.Driver -like "oem*.inf"} | 
                Select-Object Driver, @{Name = 'FileName'; Expression = {$_.OriginalFileName | Split-Path -Leaf}},
                              ClassDescription, ProviderName, Date, Version

Write-Host "All installed third-party drivers" -ForegroundColor Yellow
$HashTable | Sort-Object ClassDescription | Format-Table

Upvotes: 2

TobyU
TobyU

Reputation: 3908

Another solution would be a RegEx one:

$FullPath = "C:\Windows\System32\DriverStore\FileRepository\pantherpointsystem.inf_amd64_bde4cf569a728803\pantherpointsystem.inf"
$FullPath -match '.*\\(.*)$'
$Required = $matches[1]

.*\\(.*)$ matches all chars after the last dash \ and before the end of the line $

Upvotes: 0

Vivek Kumar Singh
Vivek Kumar Singh

Reputation: 3350

Try this -

$FullPath = "C:\Windows\System32\DriverStore\FileRepository\pantherpointsystem.inf_amd64_bde4cf569a728803\pantherpointsystem.inf"
$Required = $FullPath.Split("\")[-1]

Upvotes: 0

Related Questions