locked
get-childitem issue RRS feed

  • Question

  • I am trying to find the desktop icons for users on remote machines.  This code works.

    $PC = "RemotePC"
    $user1 = "SmithSam"
    
    Invoke-Command -ComputerName $PC -ScriptBlock {
    $UserDesktop = (Get-ChildItem -Path "c:\Users\SmithSam\Desktop").name | Sort-Object -Descending 
    $PublicDesktop = (Get-ChildItem -Path "C:\Users\Public\Desktop").name | Sort-Object -Descending
    $UserDesktop + $PublicDesktop }
     
    But I need to use a variable for the $user1. And nothing I've tried below works.  
    
    $PC = "RemotePC"
    $user1 = "SmithSam"
    
    Invoke-Command -ComputerName $PC -ScriptBlock {
    $UserDesktop = (Get-ChildItem -Path "c:\Users\$user1\Desktop").name | Sort-Object -Descending 
    $PublicDesktop = (Get-ChildItem -Path "C:\Users\Public\Desktop").name | Sort-Object -Descending
    $UserDesktop + $PublicDesktop }
    # this only returns the public desktop shortcuts
    
    # #############################################################################
    
    Invoke-Command -ComputerName $PC -ScriptBlock {
    $UserDesktop = (Get-ChildItem -Path "c:\Users\$($user1)\Desktop").name | Sort-Object -Descending 
    $PublicDesktop = (Get-ChildItem -Path "C:\Users\Public\Desktop").name | Sort-Object -Descending
    $UserDesktop + $PublicDesktop }
    # this only returns the public desktop shortcuts
    
    # #############################################################################
    
    Invoke-Command -ComputerName $PC -ScriptBlock {
    $UserDesktop = (Get-ChildItem -Path "\\$PC\c$\Users\$($user1)\Desktop").name | Sort-Object -Descending 
    $PublicDesktop = (Get-ChildItem -Path "C:\Users\Public\Desktop").name | Sort-Object -Descending
    $UserDesktop + $PublicDesktop }
    # this only returns the public desktop shortcuts

    so how do you do this using a variable for the users name? 


    mqh7

    Tuesday, October 16, 2018 9:35 PM

All replies

  • Is more easy, if you have the right permission

    $pc='remotepcname or ip address'
    $user1="the user"
    $UserDesktop = (Get-ChildItem -Path "\\$PC\c$\Users\$user1\Desktop").name | Sort-Object -Descending 

    Better

     (Get-ChildItem -directory -Path "\\$PC\c$\Users").name | Sort-Object -Descending |
        Foreach-object {
               $user=$_
               (Get-ChildItem -Path "\\$PC\c$\Users\$user\desktop").name | 
                    Foreach-object {
                            "$pc $user $_"
                    }
        } 


    Gastone Canali >http://www.armadillo.it


    Se alcuni post rispondono al tuo quesito(non necessariamente i miei), ricorda di contrassegnarli come risposta e non dimenticare di contrassegnare anche i post utili. GRAZIE! Ricorda di dare un occhio ai link Click Here andHere



    Tuesday, October 16, 2018 9:54 PM
  • See this for the answer and details: Remote Desktop Shortcuts

    \_(ツ)_/

    Wednesday, October 17, 2018 12:35 AM