powershell转换为字符串

时间:2012-05-09 19:08:25

标签: powershell

$ProfilePath = "HKLM:\software\microsoft\windows NT\currentversion\profilelist"
$ProfileList = ls -path $ProfilePath
foreach ($guid in $ProfileList)
{
    $gpath = $guid -replace "HKEY_LOCAL_MACHINE","HKLM:"
    $userpath = Get-ItemProperty -path "$gpath" | select ProfileImagePath
    $SUserName = [system.string]::$userpath
    $username = $userpath -replace "ProfileImagePath=C:\\Users\\",""
}

目前$userpath = @{ProfileImagePath=C:\Windows\ServiceProfiles\NetworkService。我需要把它变成一个字符串,以便我可以进行比较。如何将其转换为字符串?我对$SUserName的尝试生成blanknull string

3 个答案:

答案 0 :(得分:1)

您可以将路径作为字符串获取:

$ProfilePath = "HKLM:\software\microsoft\windows NT\currentversion\profilelist\*"
Get-ItemProperty $ProfilePath | foreach {$_.ProfileImagePath}

或使用拆分路径仅提取用户名

Get-ItemProperty $ProfilePath | foreach {Split-Path $_.ProfileImagePath -Leaf}

答案 1 :(得分:0)

也许我不明白你的问题。

但我认为你正在寻找这个:

$userpath.ProfileImagePath

答案 2 :(得分:0)

试试这个:

 $key = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\*'
 $key1 = (Get-ItemProperty -Path $key -Name ProfileImagePath).ProfileImagePath