pop-up in a powershell script

This topic contains 3 replies, has 4 voices, and was last updated by Profile photo of Curtis Smith Curtis Smith 3 months, 2 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #43154
    Profile photo of Jeremy Smit
    Jeremy Smit
    Participant

    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 ?

    #43160
    Profile photo of Alfano Greg
    Alfano Greg
    Participant

    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!

    #43162
    Profile photo of Solomon Acquah
    Solomon Acquah
    Participant

    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: http://blog.danskingdom.com/powershell-multi-line-input-box-dialog-open-file-dialog-folder-browser-dialog-input-box-and-message-box/

    #43501
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    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
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.