2012-05-18 166 views
1

我试图运行Powershell脚本来通过注册表清除运行历史记录。它工作的很好,但我遇到的问题是我希望它显示注册表值数据,但我无法让它正确显示。下面是该脚本:删除Powershell运行历史记录

function Delete 
{ 
$Reg = Get-RegistryValues 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU' 
foreach ($Value in $Reg) 
    { 
    $Item = Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU\' -name $Value 
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","" 
    $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","" 
    $choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no) 
    $caption = "Warning!" 
    $message = ("Do you want to delete the run value "+$Item) 
    $result = $Host.UI.PromptForChoice($caption,$message,$choices,0) 
    if($result -eq 0) 
     {    
     Remove-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU' -name $Value 
     } 
    if($result -eq 1) { } 
    } 
} 

function Get-RegistryValues($Key) 
{ 
(Get-Item $Key).GetValueNames() 

} 

Delete 

每当我尝试运行此我得到以下输出为$消息

Do you want to delete the run value @{MRULIST=idhgfcaeb} 

有谁知道的方式得到公正的数值数据,所以它会是:

idhgfcaeb 

工作溶液:

function Delete 
{ 
$Reg = Get-RegistryValues 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU' 
foreach ($Value in $Reg) 
    { 
    if ($Value -eq 'MRUList') {Set-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU\' -name $Value -value ' '} 
    Else 
     { 
     $Item = (Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU\' -name $Value).$Value.TrimEnd("\1") 
     $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","" 
     $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","" 
     $choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no) 
     $caption = "Warning!" 
     $message = ("Do you want to delete the run value "+$Item) 
     $result = $Host.UI.PromptForChoice($caption,$message,$choices,0) 
     if($result -eq 0) 
      {    
      Remove-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU' -name $Value 
      } 
     if($result -eq 1) { } 
     } 
    } 
} 

function Get-RegistryValues($Key) 
{ 
(Get-Item $Key).GetValueNames() 
} 

Delete 

回答

3

你可以使用:

$Item = (Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU\' -Name mrulist).MRUList 

或者:

("Do you want to delete the run value " + $Item.MRUList) 
+0

使用$项目$值给了我一个细分版本。我想知道是否有什么可以完成的\ 1在最后: 现在它来了:cmd \ 1 mspaint \ 1的值。 – Steve

+0

什么变量给你这个? –

+0

当我有$ Item。$ Value时,它将\ 1添加到出现的所有内容的末尾。我在那里有一个$ _。TrimEnd来照顾那个。 – Steve