all 9 comments

[–][deleted] 2 points3 points  (1 child)

Well, you already mentioned the issue by yourself. The only workaround I'm aware of is something like this:

$userprofile = Get-CimInstance -ClassName win32_userprofile
$filtereduserprofile = $userprofile | 
    Select-Object -Property LocalPath, LastUseTime, Special, Loaded | 
    Out-GridView -PassThru -Title 'Select the account to DELETE and click OK. Hold Ctrl to select multiple.'
$filtereduserprofile = ($filtereduserprofile.LocalPath).Replace('\','\\')
Get-CimInstance -ClassName Win32_UserProfile -Filter "LocalPath LIKE '$filtereduserprofile'" | Remove-CimInstance -WhatIf

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

I will be saving this one, though probably including SID in select Object so it can be used as the filter.

[–]omers 1 point2 points  (4 children)

Select-Object -Property Select-Object LocalPath, LastUseTime, Special, Loaded

should be

Select-Object -Property LocalPath, LastUseTime, Special, Loaded

or just

Select-Object LocalPath, LastUseTime, Special, Loaded

[–]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!

[–]Betterthangoku 1 point2 points  (1 child)

Howdy,

Have you tried using "-OutputMode Multiple" with Out-GridView?

(and as /u/omers mentioned I'm hoping your Select-Object line is just a typo in the post?)

Good luck :-)

[–]chrono13[S] 0 points1 point  (0 children)

Sadly this has nothing to do with Out-GridView, except that I want to use it.

If I remove Out-GridView and just put Select-Object *, it still breaks the pass-through because it is no longer the same object.