Windows powershell displays the current variable in the pipeline during a for each loop

Im deleting profiles using the get-wmi cmdlet. Since the code goes through each delete command in a loop, I would like to show which profile is being deleted. I tried this:

$total = 0
$count = 1
foreach ($localpath in $dispaths)
{$total = $total + 1}
Foreach ($localpath in $dispaths) 
{cls
write-host "Deleting Profile: $_.localpath ($count of $total)"
$count = $count + 1
get-wmiobject -class win32_userprofile -computername $cname | where 
{$_.localpath -eq $localpath.localpath} | foreach {$_.Delete()}
}

but while the counter is working correctly, the display line is displayed literally:

Delete profile: ./ localpath (1 of 135)

instead of displaying any current line inside the localpath variable. I tried to delete. from $ ._ localpath, but here is something like this:

Delete profile: (1 of 135)

it does not display anything where the variable string should stand. Where am I mistaken?

+4
source share
1 answer

ForEach, ForEach-Object. , ForEach.

ForEach:

Foreach ($localpath in $dispaths) 

, $localpath not $_

write-host "Deleting Profile: $_.localpath ($count of $total)"

:

write-host "Deleting Profile: $($Localpath.localpath) ($count of $total)"

, , ForEach ForEach-Object. , :

Get-WMIObject -Class Win32_UserProfile -ComputerName $cname | 
    Where-Object {$_.localpath -eq $localpath.localpath} |
    ForEach-Object {$_.Delete()}

, ForEach-Object, $_ . $localpath ForEach.

+3

Source: https://habr.com/ru/post/1679275/


All Articles