Powershell Profile Removal - Windows 7


I am running the below script on Windows 10 without problems. So i am attempting to run it on Windows 7. However I get this weird error. The piece before the pipe to Remove-CimInstance works fine. Just something wrong when it hits the pipe

Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq $pullUser } | Remove-CimInstance

However, i am getting the error below

Remove-CimInstance : The parameter is incorrect. 
At line:1 char:104
+ ... LocalPath.split('\')[-1] -eq "useridhere" } | Remove-CimInstance
+                                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (Win32_UserProfi...6-100815088...):CimInstance) [Remove-CimInstance], CimException
    + FullyQualifiedErrorId : HRESULT 0x80070057,Microsoft.Management.Infrastructure.CimCmdlets.RemoveCimInstanceCommand


So as it turns out, there was an issue with a WMI on the device i was testing on. I originally created this script for Win10 and have been moving backwards to make it work on Windows 7. The premise of the entire script is to create a TASK JOB to delete a user profile on Startup.

For whatever reason, the script in its entirety works fine when running from a Powershell ISE Window with Admin rights, the TASK JOB is set to run as system, however it is failing the most basic of commands (almost as if the cmdlets are not loaded). I will be posting a new question on this if I cannot figure out what the problem is.

asked on Stack Overflow Sep 25, 2020 by Aidage • edited Sep 28, 2020 by Aidage

1 Answer


Tyr this to see all details of this action.

# Get the stack information and the full error message if any
Trace-Command -Name metadata,parameterbinding,cmdlet -Expression {
    Get-CimInstance -Class Win32_UserProfile | 
    Where-Object { 
    $_.LocalPath.split('\')[-1] -eq 'TestUser'} | 
    Remove-CimInstance -Verbose -WhatIf
} -PSHost
$Error[0] | 
Select-Object -'*' | 
Format-List -Force
answered on Stack Overflow Sep 26, 2020 by postanote

User contributions licensed under CC BY-SA 3.0