PowerShell - Get FULL TargetPath of Shortcut

Page 2 of 3 FirstFirst 123 LastLast

  1. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #11

    Hello @das10,

    das10 said:
    The replacement should have been for this block ONLY (as in your original post) . . . I seem to have added an extra line when edit pasting over the old . . .
    NO problems, now changed, thanks for the follow up.
      My Computer


  2. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #12

    I have been mucking around for hours trying to find out how to Maximize the Out-GridView Window. The best option appeared to be something like Set-WindowState -Maximized, bit I can NOT get it to work.

    I have also tried numerous other options.

    I then came across this which said it can NOT be done . . .

    > Out-GridView in PowerShell: How to Change the Initial Size of the Window?

    Is it possible or NOT please before I spend any more time on it.

    Thanks.
      My Computer


  3. Posts : 3,272
    Win10
       #13

    I too can't find any options re the GridView Window. But, if for eg. say give the GridView Window the title say "
    "Get DesktopShortcuts", then by using the Sendkeys method I can maximize the GridView within a second.

    My script, which gets the targets for my own "Desktop" ("$env:USERPROFILE\Desktop\*")
    Code:
    Function Get-DesktopShortcuts{
      $Shortcuts = Get-ChildItem -Recurse "$env:USERPROFILE\Desktop\*" -Include *.lnk -force
      $Shell = New-Object -ComObject WScript.Shell
      ForEach ($Shortcut in $Shortcuts)
      {
        $Properties = @{
        ShortcutName = $Shortcut.Name;
        ShortcutFull = $Shortcut.FullName;
        ShortcutPath = $shortcut.DirectoryName;
        Target       = $Shell.CreateShortcut($Shortcut).TargetPath + " " + $Shell.CreateShortcut($Shortcut).Arguments
            }
      New-Object PSObject -Property $Properties
      }
      [Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null
    }
    $Output = Get-DesktopShortcuts
    $Output | Out-GridView  -Title "Get DesktopShortcuts"
    
    Start-Sleep -M 500
    # Start-Process -FilePath $Exe -WorkingDirectory $Path 
    $wsh = New-Object -ComObject Wscript.Shell 
    $wsh.AppActivate("Get DesktopShortcuts")
    Start-Sleep -M 250 
    $wsh.SendKeys("% ") 
    Start-Sleep -M 250 
    $wsh.Sendkeys('x')
    pause
    Not very straightforward, but hope it works for you, as testing from within Powershell_ISE works, but now testing on the Powershell consol (seems to be working).
    Last edited by das10; 15 Sep 2021 at 11:41. Reason: Correction; pause to prevent closing GridView
      My Computers


  4. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #14

    Hello @das10,

    Yes, that worked thank you very much. I will put that bit of code in my newly created PS Vault.

    In the end I have decided on the following [ one combined Arguments and one separate Arguments ] as it also closes the CMD Window when I close the Out-GridView Window . . .

    Code:
    
    Function Get-DesktopShortcuts{
      $Shortcuts = Get-ChildItem -Recurse $Env:SystemDrive\System-Admin_ToolBar -Include *.lnk
      $Shell = New-Object -ComObject WScript.Shell
      ForEach ($Shortcut in $Shortcuts)
      {
        $Properties = @{
        ShortcutName = $Shortcut.Name;
        ShortcutFull = $Shortcut.FullName;
        ShortcutPath = $shortcut.DirectoryName;
        Target       = $Shell.CreateShortcut($Shortcut).TargetPath + " " + 
                       $Shell.CreateShortcut($Shortcut).Arguments
        }
        New-Object PSObject -Property $Properties
      }
      [Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null
    }
    $Output = Get-DesktopShortcuts
    $Output | Out-GridView -Title "System-Admin Toolbar Shortcuts - Combined Target & Arguments" -PassThru
    $Output | Export-CSV $Env:UserProfile\Desktop\'Sys Admin TB Shortcuts Combined.ods'

    Code:
    
    Function Get-DesktopShortcuts{
      $Shortcuts = Get-ChildItem -Recurse $Env:SystemDrive\System-Admin_ToolBar -Include *.lnk
      $Shell = New-Object -ComObject WScript.Shell
      ForEach ($Shortcut in $Shortcuts)
      {
        $Properties = @{
        ShortcutName = $Shortcut.Name;
        ShortcutFull = $Shortcut.FullName;
        ShortcutPath = $shortcut.DirectoryName;
        Target       = $Shell.CreateShortcut($Shortcut).TargetPath;
        Arguments    = $Shell.CreateShortcut($Shortcut).Arguments;
        }
        New-Object PSObject -Property $Properties
      }
      [Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null
    }
    
    $Output = Get-DesktopShortcuts
    $Output | Out-GridView -Title "System-Admin Toolbar Shortcuts - Separate Target & Arguments" -PassThru
    $Output | Export-CSV $Env:UserProfile\Desktop\'Sys Admin TB Shortcuts Separate.ods'

    Over the weekend I will carry on trying to get the Arguments column AFTER the TargetPath column in the Out-GridView in the SECOND file.

      My Computer


  5. Posts : 3,272
    Win10
       #15

    When you have time you could test by changing line no 13 in the second ps1
    from:
    Code:
                New-Object PSObject -Property $Properties
    to:
    Code:
                New-Object PSObject -Property $Properties | Select ShortcutName,ShortcutFull,ShortcutPath,Target,Arguments
    to get the Order of display ?
      My Computers


  6. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #16

    Hello @das10,

    That works perfectly THANK YOU.

    One FINAL question please. I would ideally like the columns to AutoSize, would that be the same section as above please?
      My Computer


  7. Posts : 3,272
    Win10
       #17

    I have no idea about that. Maybe its time to call in one of the Powershell experts like @KeithM.
      My Computers


  8. Posts : 3,272
    Win10
       #18

    Paul:
    In a quest to try and autosize cells and maximize the Gridview Window and a few spare hours to boot, I ran into another alternative, based on a custom dialog box 'gridview' function for you to play with.

    Made GridView to close with a single F4 key (around line 76) or Escape key if wanted (around line 84) or Alt+F4. So, if there is no pause command in the batch file, closing the GridView also closes the Command Box. The font/font-size can also be changed if desired.

    Hopefully, you may be able to debug any issues.

    Code:
    # main Code based on:
    # https://social.technet.microsoft.com/Forums/windowsserver/en-US/edf7c616-a201-4076-8025-b9ef02bcbd0e/wrap-text-in-grid-column-of-outgridview?forum=winserverpowershell
    # Function out-datagridView
    #
    # shows piplineinput in a GUI using a datagridView
    # and returns the given field on double-Click or Enter
    # original source maybe MS devblogs -scripting
    # /\/\o\/\/ 2006
    # http://ThePowerShellGuy.com
    
    # Load the System.Windows.Forms assembly into PowerShell  - line needed to run in consol as well
    Add-Type -AssemblyName System.Windows.Forms
    
    # Create a new Form object and assign to the variable $Form
    $Form = New-Object System.Windows.Forms.Form
    
    #Initialize Form so it can be seen
    # $Form.showDialog()
    
    
    Function out-datagridView ([String]$ReturnField)
    {
     # Make DataTable from Input
     $dats = new-object Data.datatable
     $First = $true
     foreach ($item in $input){
      $DatsRow = $Dats.NewRow()
      $Item.PsObject.get_properties() | foreach {
       If ($first) {
        $Col = new-object Data.DataColumn
        $Col.ColumnName = $_.Name.ToString()
        $Dats.Columns.Add($Col)    }
       if ($_.value -eq $null) {
        $DatsRow.Item($_.Name) = "[empty]"
       }
       ElseIf ($_.IsArray) {
        $DatsRow.Item($_.Name) =[string]::Join($_.value ,";")
       }
       Else {
        $DatsRow.Item($_.Name) = $_.value
       }
      }
      $Dats.Rows.Add($DatsRow)
      $First = $false
     }
     # show Datatable in Form
         $form = new-object Windows.Forms.form
    
         $form.WindowState = 'Maximized'
    
    # change font size and style
         $Font = New-Object System.Drawing.Font("Segoe UI",11,[System.Drawing.FontStyle]::Regular)
         $Form.Font = $Font
              
    # to show full screen use
    # $form.FormBorderStyle = 'None'
         
     $DatsGrid = new-object windows.forms.DataGridView
     $DatsGrid.DataSource = $Dats.psObject.baseobject
     $DatsGrid.Dock = [System.Windows.Forms.DockStyle]::Fill
     $DatsGrid.RowHeadersWidthSizeMode = [System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode]::EnableResizing
     	
     # Enable wrap mode on the cells and make the grid autosize the rows when the columns wrap
    
     #                                                                                 \/\/\/
     $DatsGrid.defaultCellStyle.wrapMode= [System.Windows.Forms.DataGridViewTriState]::FALSE
     $DatsGrid.AutoSizeRowsMode = [System.Windows.Forms.DataGridViewAutoSizeRowsMode]::AllCells
     	
     $DatsGrid.SelectionMode = 'FullRowSelect'
    
      <#   
      $DatsGrid.add_DoubleClick({
      $script:ret = $this.SelectedRows |% {$_.DataboundItem["$ReturnField"]}
      $form.Close()
      })
      #>
    
     $form.text = "Powershell - Get Extra Details About Shortcuts ( On The Desktop In This Instance )"
     $form.KeyPreview = $true
     $form.Add_KeyDown({
       if ($_.KeyCode -eq 'F4') {
       $form.Close()
      }
    
    #  so escape key closes gridview
       ElseIf ($_.KeyCode -eq 'Escape'){
       $form.Close()
      }
     })
    
     $form.Controls.Add($DatsGrid)
     $Form.Add_Shown({$form.Activate();$DatsGrid.AutoResizeColumns()})
     $script:ret = $null
     [void]$form.showdialog()
     $script:ret
    }
    
    # Your section of code begins here
    
         Function Get-DesktopShortcuts{
         $Shortcuts = Get-ChildItem -Recurse -Force "$env:PUBLIC\Desktop\*" , "$env:USERPROFILE\Desktop\*" -Include '*.lnk'
         $Shell = New-Object -ComObject WScript.Shell
         ForEach ($Shortcut in $Shortcuts)
         {
    
         $Properties = @{
         ShortcutName         = $Shortcut.Name
         ShortcutFullPathName = $Shortcut.FullName
         ShortcutPath         = $Shortcut.DirectoryName
         Target               = $Shell.CreateShortcut($Shortcut).TargetPath
         Arguments            = $Shell.CreateShortcut($Shortcut).Arguments
         Description          = $Shell.CreateShortcut($Shortcut).Description
         Hotkey               = $Shell.CreateShortcut($Shortcut).Hotkey
         WindowStyle          = $Shell.CreateShortcut($Shortcut).WindowStyle
         IconLocation         = $Shell.CreateShortcut($Shortcut).IconLocation
         WorkingDirectory     = $Shell.CreateShortcut($Shortcut).WorkingDirectory
    
         } 
       # New-Object PSObject -Property $Properties | Select ShortcutName,ShortcutFull,ShortcutPath,Target,Arguments,Description
         New-Object PSObject -Property $Properties | Select ShortcutPath,ShortcutName,ShortcutFullPathName,Target,Arguments,Description,WorkingDirectory,Hotkey,WindowStyle,IconLocation
         }
         [Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null
         }
    
         $Output = Get-DesktopShortcuts
     
    # To export to CSV file use line below ; & get rid of first line in csv #TYPE Selected.System.Management.Automation.PSCustomObject                                                          \/\/\/\/\/\/\/\/
         # $Output | Export-CSV $Env:UserProfile\Desktop\'Sys Admin TB Shortcuts Separate.csv' -NoTypeInformation
    
    # To use PS's own Out-GridView use the "two lines" below
         # $Output | Out-GridView -Title "Powershell - Get Extra Details About Shortcuts ( On The Desktop In This Instance )"
         # pause
    
    # To use Custom Grid-View ( using function out-datagridView ) instead of PS's own Out-GridView use the line below
         $Output | out-datagridView
    Last edited by das10; 01 Oct 2021 at 03:51.
      My Computers


  9. Posts : 456
    Windows 10
       #19

    Can it also be a batch | VB Script hybrid instead of powershell?
      My Computer


  10. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #20

    Hello @ricardobohner,

    ricardobohner said:
    Can it also be a batch | VB Script hybrid instead of powershell?
    Yes, there is NO reason why NOT. That would make a nice selection of options.

    I am hoping to do some more work on this very soon.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 18:50.
Find Us




Windows 10 Forums