0

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.

farag
  • 325
  • 1
  • 4
  • 20
  • 1
    Possible duplicate of [Extract the filename from a path](https://stackoverflow.com/questions/35813186/extract-the-filename-from-a-path) – iRon Nov 22 '18 at 07:34

3 Answers3

2

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
Theo
  • 57,719
  • 8
  • 24
  • 41
0

Try this -

$FullPath = "C:\Windows\System32\DriverStore\FileRepository\pantherpointsystem.inf_amd64_bde4cf569a728803\pantherpointsystem.inf"
$Required = $FullPath.Split("\")[-1]
Vivek Kumar Singh
  • 3,223
  • 1
  • 14
  • 27
0

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 $

TobyU
  • 3,718
  • 2
  • 21
  • 32