Page 1 of 1

creating a package to delete user profiles

Published: August 30, 2022 - 1:53 PM
by adgm11
Good morning
I have a PowerShell command to delete user profiles except for admin accounts. However, after the task completes correctly and deletes all the specified profiles, there are errors related to "Remove-CimInstance access denied".
The package is this one:

Code: Select all

def install():
    run_powershell('Set-ExecutionPolicy Unrestricted')
    run_powershell('$AccountsToKeep = @("admin","administrator","Public","default")')
    run_powershell("Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -notin $AccountsToKeep } | Remove-CimInstance")

Is there a way to convert this command to Python? Or can we eliminate PowerShell errors?

Re: Creating a package to delete user profiles

Published: August 30, 2022 - 2:21 PM
by dcardon
As it's written, the different PowerShell commands will be executed individually without preserving the context between them. So it won't work. You can try putting everything in a ps1 file.
Otherwise, the best solution would indeed be to rewrite it in Python; it will be much cleaner.
Regards,
Denis