question

drClays avatar image
0 Votes"
drClays asked drClays commented

Get-ChildItem do not working with computers form csv

Hello,

I have a problem with the command Get-ChildItem where I import remote computers from CSV.

CSV file:
Name
computer1
computer2
computer3

Powershell:

 $compsPath = "C:\temp\comps.csv"
 $comps = Import-Csv $compsPath
 foreach ($comp in $comps)
 {
    
     $remotePath = "\\$comp\c$\Users\Public\Desktop"
     Get-ChildItem -Path $rmPath * -Include *.lnk -Recurse
        
 }


When I use it I've got an error:

 Get-ChildItem : Cannot find path '\\@{Name=computer1}\c$\Users\Public\Desktop' because it does not exist.
 At line:11 char:5
 +     Get-ChildItem -Path $remotePath * -Include *.lnk -Recurse
 +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : ObjectNotFound: (\\@{Name=domain-...\Public\Desktop:String) [Get-ChildItem], ItemNotFoundExceptio 
    n
     + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

But when I use

 Get-ChildItem -Path \\computer1\c$\Users\Public\Desktop * -Include *.lnk -Recurse

It's working...

Any suggestions?


windows-serverwindows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

StoyanChalakov avatar image
0 Votes"
StoyanChalakov answered drClays commented

Hey,

this is because your CSV has a header - "Name"...Please try like this:

  $compsPath = "C:\temp\comps.csv"
  $comps = Import-Csv $compsPath
  foreach ($comp in $comps.Name)
  {
        
      $remotePath = "\\$comp\c$\Users\Public\Desktop"
      Get-ChildItem -Path $rmPath * -Include *.lnk -Recurse
            
  }

Does it work like this? With me it did :)


If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Regards,
Stoyan


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hey,

OMG, I'm stupid and forgot it...

Now it's working...

Thanks a lot :)

0 Votes 0 ·