Reputation: 97
Im deleting profiles using the get-wmi commandlet. As the code goes through each delete command in the loop I would like to display 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 count works right, the display line shows literally:
Deleting Profile: ./localpath (1 of 135)
instead of displaying whatever the current string inside of the localpath variable is. I tried removing the . from $._localpath but that just displayed something like this:
Deleting Profile: (1 of 135)
it doesnt display anything where the variable string should be. Where am I going wrong?
Upvotes: 4
Views: 2083
Reputation: 10044
The loop is a ForEach
loop not a ForEach-Object
. So you need to use the variable defined in the ForEach
Statement.
Your Foreach
:
Foreach ($localpath in $dispaths)
So you'll need to properties on the variable $localpath
not $_
write-host "Deleting Profile: $_.localpath ($count of $total)"
would become:
write-host "Deleting Profile: $($Localpath.localpath) ($count of $total)"
Note that this is different from your final line, as your are using foreach
as the alias for ForEach-Object
. So your line could more precisely written as:
Get-WMIObject -Class Win32_UserProfile -ComputerName $cname |
Where-Object {$_.localpath -eq $localpath.localpath} |
ForEach-Object {$_.Delete()}
So since this is line uses ForEach-Object
and the pipeline you use $_
for the values pass down the pipeline. But also uses $localpath
from the parent ForEach
loop.
Upvotes: 3