Producing a list of files on remote computer desktops

I need to produce a list of files stored on multiple remote computer desktops.

Can some provide a powershell script so i can do this from my computer.

August 21st, 2015 8:28am

You will use Get-ChildItem, and since you are doing remote computers you will need to be an Administrator as you will need to access the admin shares

Get-Help Get-ChildItem -Full

# Example

Get-ChildItem \\computername\c$\SomeFolder -Recurse		
Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 8:48am

in addition to clyman2s post, something like this:

$Computers = 'localhost', 'Computer2'

foreach ($Computer in $Computers){
    $Users = (Get-ChildItem -Path \\$Computer\C$\Users).Name
    foreach ($User in $Users){
        Get-ChildItem -Path \\$Computer\C$\Users\$User\Desktop -Recurse
    }
}

it's up to you, how you want to format it

August 21st, 2015 8:55am

And with the following you will retrieve the loggedon User.

$Computers = 'localhost'

foreach ($Computer in $Computers){

    #Gets the logged on user
    $User = Get-WmiObject win32_logonsession -ComputerName $Computer -Filter "Logontype = '2' or Logontype='11' or logontype='10'" |
                foreach {Get-WmiObject win32_loggedonuser -ComputerName $Computer -filter "Dependent = '\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=`"$($_.logonid)`"'" | select Antecedent } |
                foreach { ($_.antecedent.split('"'))[1] + "\" + ($_.antecedent.split('"'))[3] } | select -unique

    #Catches Domain name
    if ($user -like "*\*")
    {
        $user = $user.Split('\')[-1]
    }

    $searchPath="\\$Computer\C$\Users\$User\Desktop\"
    Get-ChildItem -Path $searchPath -Recurse
}


Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 9:58am

David, thanks for the information. What if I wanted to get your second script to read the computer names from a single text or CSV file and then export the information to a text or CSV file for example. How would I do that?
August 21st, 2015 11:17am

What if I wanted to get your second script to read the computer names from a single text or CSV file and then export the information to a text or CSV file for example. How would I do that?

Use Get-Content to read a text file or Import-Csv for a CSV file. Use ForEach-Object to process each item, then use Export-Csv outside of the loop to export your results out to a CSV file.

Syntax/examples:

http://ss64.com/ps/get-content.html

http://ss64.com/ps/import-csv.html

http://ss64.com/ps/foreach-object.html

http://ss64.com/ps/export-csv.html

Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 11:24am

I have tried the first part of trying to read from a CSV file but i am getting the error below.

Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argum
ent is null or empty. Supply an argument that is not null or empty and then try
 the command again.
At line:3 char:59
+     $User = Get-WmiObject win32_logonsession -ComputerName <<<<  $Computer -F
ilter "Logontype = '2' or Logontype='11' or logontype='10'" |
    + CategoryInfo          : InvalidData: (:) [Get-WmiObject], ParameterBindi
   ngValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power
   Shell.Commands.GetWmiObjectCommand

Get-ChildItem : Cannot find path '\\\C$\Users\\Desktop\' because it does not ex
ist.
At line:12 char:18
+     Get-ChildItem <<<<  -Path $searchPath -Recurse
    + CategoryInfo          : ObjectNotFound: (\\\C$\Users\\Desktop\:String) [
   Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCh
   ildItemCommand

Script:

$Computers = Import-Csv c:\computers.csv

foreach ($item in $Computers){

    #Gets the logged on user
    $User = Get-WmiObject win32_logonsession -ComputerName $Computer -Filter "Logontype = '2' or Logontype='11' or logontype='10'" |
                foreach {Get-WmiObject win32_loggedonuser -ComputerName $Computer -filter "Dependent = '\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=`"$($_.logonid)`"'" | select Antecedent } |
                foreach { ($_.antecedent.split('"'))[1] + "\" + ($_.antecedent.split('"'))[3] } | select -unique

    #Catches Domain name
    if ($user -like "*\*")
    {
        $user = $user.Split('\')[-1]
    }

    $searchPath="\\$Computer\C$\Users\$User\Desktop\"
    Get-ChildItem -Path $searchPath -Recurse
}



August 21st, 2015 12:10pm

I have tried the first part of trying to read from a CSV file but i am getting the error below.

Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argum
ent is null or empty. Supply an argument that is not null or empty and then try
 the command again.
At line:3 char:59
+     $User = Get-WmiObject win32_logonsession -ComputerName <<<<  $Computer -F
ilter "Logontype = '2' or Logontype='11' or logontype='10'" |
    + CategoryInfo          : InvalidData: (:) [Get-WmiObject], ParameterBindi
   ngValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power
   Shell.Commands.GetWmiObjectCommand

Get-ChildItem : Cannot find path '\\\C$\Users\\Desktop\' because it does not ex
ist.
At line:12 char:18
+     Get-ChildItem <<<<  -Path $searchPath -Recurse
    + CategoryInfo          : ObjectNotFound: (\\\C$\Users\\Desktop\:String) [
   Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCh
   ildItemCommand

Script:

$Computers = Import-Csv c:\computers.csv

foreach ($item in $Computers){

    #Gets the logged on user
    $User = Get-WmiObject win32_logonsession -ComputerName $Computer -Filter "Logontype = '2' or Logontype='11' or logontype='10'" |
                foreach {Get-WmiObject win32_loggedonuser -ComputerName $Computer -filter "Dependent = '\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=`"$($_.logonid)`"'" | select Antecedent } |
                foreach { ($_.antecedent.split('"'))[1] + "\" + ($_.antecedent.split('"'))[3] } | select -unique

    #Catches Domain name
    if ($user -like "*\*")
    {
        $user = $user.Split('\')[-1]
    }

    $searchPath="\\$Computer\C$\Users\$User\Desktop\"
    Get-ChildItem -Path $searchPath -Recurse
}



Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 12:16pm

If you're going to use $item as the individual item in the foreach loop, you need to actually use it.
August 21st, 2015 12:36pm

Hi,

how does your csv look like?

$Computers = Import-Csv c:\computers.csv

foreach ($Computer in $Computers){

    #Gets the logged on user
    $User = Get-WmiObject win32_logonsession -ComputerName $Computer -Filter "Logontype = '2' or Logontype='11' or logontype='10'" |
                foreach {Get-WmiObject win32_loggedonuser -ComputerName $Computer -filter "Dependent = '\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=`"$($_.logonid)`"'" | select Antecedent } |
                foreach { ($_.antecedent.split('"'))[1] + "\" + ($_.antecedent.split('"'))[3] } | select -unique

    #Catches Domain name
    if ($user -like "*\*")
    {
        $user = $user.Split('\')[-1]
    }

    $searchPath="\\$Computer\C$\Users\$User\Desktop\"
    Get-ChildItem -Path $searchPath -Recurse
}

Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 1:55pm

I have tried the first part of trying to read from a CSV file but i am getting the error below.

Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argum
ent is null or empty. Supply an argument that is not null or empty and then try
 the command again.
At line:3 char:59
+     $User = Get-WmiObject win32_logonsession -ComputerName <<<<  $Computer -F
ilter "Logontype = '2' or Logontype='11' or logontype='10'" |
    + CategoryInfo          : InvalidData: (:) [Get-WmiObject], ParameterBindi
   ngValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power
   Shell.Commands.GetWmiObjectCommand

Get-ChildItem : Cannot find path '\\\C$\Users\\Desktop\' because it does not ex
ist.
At line:12 char:18
+     Get-ChildItem <<<<  -Path $searchPath -Recurse
    + CategoryInfo          : ObjectNotFound: (\\\C$\Users\\Desktop\:String) [
   Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCh
   ildItemCommand

Script:

$Computers = Import-Csv c:\computers.csv

foreach ($item in $Computers){

    #Gets the logged on user
    $User = Get-WmiObject win32_logonsession -ComputerName $Computer -Filter "Logontype = '2' or Logontype='11' or logontype='10'" |
                foreach {Get-WmiObject win32_loggedonuser -ComputerName $Computer -filter "Dependent = '\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=`"$($_.logonid)`"'" | select Antecedent } |
                foreach { ($_.antecedent.split('"'))[1] + "\" + ($_.antecedent.split('"'))[3] } | select -unique

    #Catches Domain name
    if ($user -like "*\*")
    {
        $user = $user.Split('\')[-1]
    }

    $searchPath="\\$Computer\C$\Users\$User\Desktop\"
    Get-ChildItem -Path $searchPath -Recurse
}



  • Edited by Blue_Craig Friday, August 21, 2015 4:10 PM
August 21st, 2015 4:09pm

My CV just contains one computer name at the moment for example:

computer1

Free Windows Admin Tool Kit Click here and download it now
August 22nd, 2015 2:48am

A file with one line is NOT a CSV.  A CSV has a header and it has columns. Just adding a CSV extension does not make a file a CSV file.

Please try to learn what a data file si and how to work with data.

https://en.wikipedia.org/wiki/Comma-separated_valu

August 22nd, 2015 4:11am

Hi there

CSV:

Computername
localhost
MyWonderfulComputername

And use this script. I experienced that a wrong user was returned -> DWM-1 .. DWM-n - This is a system-user since win8  - may be there is also a better way to find the logged on user account:

foreach ($c in $Computers){
    $Computer=$c.'Computername'
    #Gets the logged on user
    $User = Get-WmiObject win32_logonsession -ComputerName $Computer -Filter "Logontype = '2' or Logontype='11' or logontype='10'" |
                foreach {Get-WmiObject win32_loggedonuser -ComputerName $Computer -filter "Dependent = '\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=`"$($_.logonid)`"'" | select Antecedent } |
                foreach { ($_.antecedent.split('"'))[1] + "\" + ($_.antecedent.split('"'))[3] } | Where-Object{$_ -notlike "*DWM*"} | select -unique

    #Catches Domain name
    if ($user -like "*\*")
    {
        $user = $user.Split('\')[-1]
    }

    $searchPath="\\$Computer\C$\Users\$User\Desktop\"
    Get-ChildItem -Path $searchPath -Recurse
}

Free Windows Admin Tool Kit Click here and download it now
August 22nd, 2015 6:56am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics