Get-ChildItem do not working with computers form csv

Marcin Puwalski 101 Reputation points
2021-11-04T10:20:41.62+00:00

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 Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
8,173 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,619 questions
No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-11-04T10:48:32.137+00:00

    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


0 additional answers

Sort by: Most helpful