Hello fellow powershell admins/users,
i’m learning powershell now for like 3 months and also begon with building scripts.
now i whant to create a script, (i think it is a simple one) it is onky one line
Get-WMIObject Win32_UserProfile | ? {$_.localpath –like “username"} | Remove-Wm
iObject -whatif
but my question now is where is, is it to let say, when you run the script that it pops uo and you’ll need to fill in wich username ?
Hi Jeremy,
There are a few ways you can accomplish this. One quick way is to pipe all the results to Out-GridView with the -PassThru switch. This will send the items you choose down the pipeline (to Remove-WmiObject in your case)…
Get-WmiObject win32_userprofile | Out-GridView -PassThru | Remove-WmiObject
Another route would be to turn this into a function and use a dynamic parameter to validate your set of usernames. Dynamic parameters are a bit advanced with not a whole lot of examples out there. Here is one that got me going: https://blogs.technet.microsoft.com/pstips/2014/06/09/dynamic-validateset-in-a-dynamic-parameter/
Hope this help!
Something like this:
function Read-InputBoxDialog([string]$Message, [string]$WindowTitle, [string]$DefaultText)
{
Add-Type -AssemblyName Microsoft.VisualBasic
return [Microsoft.VisualBasic.Interaction]::InputBox($Message, $WindowTitle, $DefaultText)
}
$username = Read-InputBoxDialog -Message "Please enter the username" -WindowTitle "Enter a USername"
if ($username -eq $null) { Write-Host "You clicked Cancel" }
else { Get-WMIObject Win32_UserProfile | ? {$_.localpath –like "$username"} | Remove-WmiObject -whatif }
Taken from: PowerShell Multi-Line Input Box Dialog, Open File Dialog, Folder Browser Dialog, Input Box, and Message Box - Daniel Schroeder’s Programming Blog
If you just need to get user input and don’t necessarily need a “Pop-up” window. You can prompt for input at the prompt using read-host
$username = Read-Host "Enter target profile username"
Get-WmiObject Win32_UserProfile | Where-Object {$_.localpath -like "*$username*"} | Remove-WmiObject -WhatIf