can Read-host -prompt pop up in a new window, something like Out-GridView ?

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Hassan Serajian Hassan Serajian 5 months, 3 weeks ago.

  • Author
    Posts
  • #62665
    Profile photo of Hassan Serajian
    Hassan Serajian
    Participant

    When I prompt the user to provide input in PowerShell command window, I want to be able to prompt the user with messages in GUI pop-up windows instead of asking user to provide answers in PowerShell command window.

    I am looking for a code that does the same job as Out-GridView. (i.e. gsv | Out-GridView). is the Out-GridView code simpler than writing Windows forms with PowerShell?

    here is a sample script to backup and restore a Microsoft Edge (.EDB) database file, and Microsoft Edge Favorite bookmarks.
    Any suggestion or comment on how to improve this script in terms of making it shorter and more efficient would be greatly appreciated. Thank you

    ###Scripts only runs on Windows 10 version 10.0.10586 and above where Microsoft Edge database folder structure exists
    Set-ExecutionPolicy RemoteSigned -Force
    if((Get-WmiObject -Class win32_operatingsystem).Version -lt '10.0.10586'){
    "This PowerShell script can be used on Windows 10 version '10.0.10586' and above";Start-Sleep -Seconds 10;exit}

    $EdgeDatabaseFolder = "$env:LOCALAPPDATA\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\User\Default\DataStore\Data\nouser1\120712-0049\DBStore"
    If((Test-Path -Path $EdgeDatabaseFolder) -eq $false){
    "Open Microsoft Edge browser, and run this script again "}

    if((Get-WmiObject -Class win32_operatingsystem).Version -gt '10.0.10586'){"creating backup files"}
    #Script continues to run if the database folder structure exists, and creates a backup of Edge browser .edb file along with the #120712-0049 folder
    $EdgeDatabaseFolder = "$env:LOCALAPPDATA\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\User\Default\DataStore\Data\nouser1\120712-0049\DBStore"
    If((Test-Path -Path $EdgeDatabaseFolder) -eq $true){
    $EdgeDatabaseFile = "$env:LOCALAPPDATA\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\User\Default\DataStore\Data\nouser1\120712-0049\DBStore\spartan.edb"
    Copy-Item -Path $EdgeDatabaseFolder -Destination "$Home\Documents\Edge-Favorite-Backup-Before-Import" -Recurse
    $EdbBackUpFileName = (Get-Date -Format o) | foreach {$_ -replace ":","."}
    Rename-Item -Path $EdgeDatabaseFile -NewName (-join($EdbBackUpFileName + ".edb"))
    }

    #Closes Microsoft Edge browser intances and imports spartan.edb file from backup to restore bookmarks
    "Importing your Edge browser and Favorite bookmarks from backup which are stored in spartan.edb file..."
    $EdbBackupFileToRestore = read-host -Prompt "Enter the file path to your Edge browser database (spartan.edb) backup file (i.e. C:\users\Win10\Documents\Backup\120712-0049\DBStore\spartan.edb)"
    if((Read-Host -Prompt "pressing 'y' will close all currently open Edge browser windows and processes on the machine where this script runs. Press 'y' to continue to restore your Edge spartan.edb file from backup)") -eq 'y'){
    Get-Process MicrosoftEdge* | Stop-Process
    Copy-Item -Path $EdbBackupFileToRestore -Destination $EdgeDatabaseFolder -Force}
    if((Read-Host -Prompt "Your Edge browser bookmarks have been restored. It is recommended to Ctrl C to Exit the script here, however, to continue restoring previously cached icons and log files from backup, which may not be required, press y to continue or press Ctrl C to exit") -eq 'y'){
    $EdgeFolder = "$env:LOCALAPPDATA\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\User\Default\DataStore\Data\nouser1\120712-0049\"
    $EdgeBackupFolderToRestore = read-host -Prompt "Enter the path to your '\120712-0049\' Edge backup folder (i.e. C:\users\Win10\Documents\Backup\120712-0049)"
    $EdgeFavoritesCachedIconsDir = "$EdgeBackupFolderToRestore\Favorites"
    Copy-Item -Path $EdgeFavoritesCachedIconsDir -Destination "$EdgeFolder" -Recurse -Force
    }
    if((read-host -prompt "Restore is comlete. To roll back changes press 'R' or To exit the script press C") -eq 'r'){
    ${GetIndexOf120712-0049} = $EdgeFolder.IndexOf("120712-0049")
    ${Edge-nouser1-Folder} = $EdgeFolder.Remove(${GetIndexOf120712-0049})
    Copy-Item -Path $EdgeBackupFolderToRestore -Destination ${Edge-nouser1-Folder} -Recurse -Force -Confirm:$false
    } # End of script

    "End of script"
    "#############"

  • #62679
    Profile photo of nimms
    nimms
    Participant

    Hello, Hassan. Lookie here:

    [void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
    $Message = "Enter the path to your '120712-0049' Edge backup folder (i.e. C:\users\Win10\Documents\Backup\120712-0049)"
    $Title = "Enter the path"
    $r = ''
    while (-not $r) {
        $r = [Microsoft.VisualBasic.Interaction]::InputBox($Message, $Title)
    }
    $r

    Well, you get the idea. 🙂 You can use Visual Basic's InputBox in your PowerShell script. It gets installed with .NET Framework.

    • #62685
      Profile photo of Hassan Serajian
      Hassan Serajian
      Participant

      Thank you. yes I got it.
      1- VS XAML file and dialog scripts
      2-.NET assemblies with native code

  • #62698
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    You'd do better to make the Y/N choices switch parameters and the path a parameter rather than using prompts like this

  • #62920
    Profile photo of Hassan Serajian
    Hassan Serajian
    Participant

    Thanks. it made it shorter., something like this?

    Restore-Edge-Favorites (
    [parameter(Mandatory=$true,HelpMessage="file path to spartan.edb backup (i.e. E:\backup\120712-0049\DBStore\spartan.edb)")]$EdbBackupFileToRestore,
    [parameter(Mandatory=$true,HelpMessage="folder path to '\120712-0049\' backup (i.e. E:\backup\120712-0049)")]$EdgeBackupFolder
    ){ 
    if((Read-Host -Prompt "Close all Edge browsers, '
    then press 'Y(for Yes)' to continue or N(for No) to exit") -eq 'y'){
    Get-Process MicrosoftEdge* | Stop-Process
    $EdgeDatabaseFolder = "$env:LOCALAPPDATA\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\User\Default\DataStore\Data\nouser1\120712-0049\DBStore"
    Copy-Item -Path $EdbBackupFileToRestore -Destination $EdgeDatabaseFolder -Force}
    if((Read-Host -Prompt "Press Y(for Yes) to restore CachedIcons and logs or Press N(for No) to exit" -eq 'y')){
    ${120712-0049} = "$env:LOCALAPPDATA\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\User\Default\DataStore\Data\nouser1\120712-0049\"
    $CachedIconsDir = "$EdgeBackupFolder\Favorites"
    Copy-Item -Path $CachedIconsDir -Destination "${120712-0049}" -Recurse -Force
    }
    }
    Restore-Edge-Favorites
    

You must be logged in to reply to this topic.