在哈希表中删除Powershell中的部分字符串

时间:2018-11-22 07:19:50

标签: powershell

我有司机名单

@IBAction func backToHome(_ sender: Any) {
    navigationController?.popViewController(animated: true)
} 

该表在OriginalFileName列中显示完整的inf文件路径。我需要剪切完整路径,例如

$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

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

所有行都这样。

3 个答案:

答案 0 :(得分:2)

要从完整路径中拆分文件名,可以使用Powershells Split-Path cmdlet,如下所示:

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

或这样使用.NET:

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

在您的情况下,我将使用计算所得的属性来填充哈希表:

$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

答案 1 :(得分:0)

尝试一下-

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

答案 2 :(得分:0)

另一种解决方案是RegEx

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

.*\\(.*)$匹配最后一个破折号\之后和行$

之前的所有字符。