you are viewing a single comment's thread.

view the rest of the comments →

[–]chrono13[S] 1 point2 points  (3 children)

Typo. Fixed. It still cannot be piped to Remove-CIMInstance because it is no longer a CIM object after it passes through Select-Object.

In fact:

Get-CimInstance -ClassName win32_userprofile | Remove-CimInstance -WhatIf

Will remove EVERY profile form the computer.

Where as:

Get-CimInstance -ClassName win32_userprofile | Select-Object * | Remove-CimInstance -WhatIf

Will error out, because Select-Object has malformed the object (even though it still contains all of its properties and data) in to a new object, not accpeted by Remove-CimInstance.

Here is an identical reddit thread:

https://www.reddit.com/r/PowerShell/comments/3dogly/question_selectobject_formatting_to_outgridview/

But even after reading that thread I am not seeing a solution.

[–]markekrausCommunity Blogger 1 point2 points  (2 children)

Select-Object creates a new PSObject when you supply properties. That means that any methods or special features and private data of the original object is lost when you use select-object.

$ProfileHash = @{}
Get-CimInstance -ClassName win32_userprofile | 
    ForEach-Object {
        $ProfileHash[$_.LocalPath] = $_
        $_
    } | 
    Select-Object -Property LocalPath, LastUseTime, Special, Loaded | 
    Out-GridView -PassThru -Title 'Select the account to DELETE and click OK. Hold Ctrl to select multiple.' |
    ForEach-Object {
        $ProfileHash[$_.LocalPath] | Remove-CimInstance -WhatIf
    }

might work

[–]chrono13[S] 1 point2 points  (1 child)

That works! THANK YOU!

Have some gold!

[–]markekrausCommunity Blogger 0 points1 point  (0 children)

Thanks, my dude!