Adding a string to a list returns the length of the string

I am a beginner at Powershell. I have a script that I’m using to copy a file to a list of computers.

The problem I’m running into is that when I add a string representing a computer name to the list ends up being a number that is the length of the string.

Here is a copy of the script:

$csv = Import-Csv “C:\Users\michaelk\Downloads\staffcomputers.txt”
$Source = “c:\keeper\KeeperPasswordManager.appinstaller”
$dest = "C$\Keeper"
$Output = “C:\Keeper\missingworkstations.csv”
$Items = New-Object System.Collections.Generic.List[System.Object]

#reads .csv for workstation NAME.
foreach ($line in $csv)
{
#pings each Host. If true, Copy file.
if (Test-Connection $line.Name -count 1 -quiet)
{
write-Host “true”, $line.Name
$name = “\” + $line.Name + "\C$\keeper"

      #copies the file over to target machine
      new-Item -Path "$name" -ItemType Directory -Force
	  Copy-Item -path $Source -Destination $name
   }

#if ping fails, log which workstation and that workstation’s IP in a new CSV.
else
{
write-host “false”, $line.Name
$items.add($line.Name)
}
}
#exports array of workstations that were unreachable for manual processing at a later date.
$items | Export-Csv -NoTypeInformation -Path $Output

Where the last Write-Host it returns the actual name of the computer, but then on the next line I get the length of the string. How can I fix this?

Michael,
Welcome to the forum. :wave:t4:

When you post code or sample data or console output please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance

Something like this shouild work actually.

$csv = Import-Csv -Path 'C:\Users\michaelk\Downloads\staffcomputers.txt'
$Source = 'c:\keeper\KeeperPasswordManager.appinstaller'
$Output = 'C:\Keeper\missingworkstations.csv'
$Items = New-Object System.Collections.Generic.List[System.Object]

foreach ($line in $csv) {
    if (Test-Connection $line.Name -count 1 -quiet) {
        $name = '\\{0}\C$\keeper' -f $line.Name 
        New-Item -Path $name -ItemType Directory -Force | Out-Null
        Copy-Item -path $Source -Destination $name
    }
    else {
        $items.add($($line.Name))
    }
}
$items | Export-Csv -Path $Output -NoTypeInformation

Sorry, but it still isn’t working. This is the output I received in C:\keeper\missingworkstations.csv:

“Length”

“12”

“13”

“15”

“10”

“13”

“14”

“14”

“15”

“9”

“9”

“8”

“11”

“9”

“11”

“14”

“11”

Does the directory creation and file copy part work?

Yes that part works.

Add a header to that export

attach a sample of the staffcomputers.txt so we can see how it is formatted

Here you go.

(Attachment StaffComputers.txt is missing)

God Bless,

Michael Kerrick
IT Manager Holy Family Parish and School
M: 425 786-4318
D: 425 947-7859

When sending to a large distribution list, please put the list on the BCC line.
This will prevent people from Replying All
It will alleviate extra email flooding into our Inboxes.

(Attachment StaffComputers.txt is missing)

For some reason it would not let me send the file, but I think it worked at the end after I resaved the file.

Let me know if you didn’t get it.

Name
CARTA-002
CARTA-003
CARTA-004
CARTA-005
CARTA-006
CARTA-007
CARTA-008
CARTA-009
CARTA-010
CARTA-011
CARTA-012
CARTA-013
CARTA-014
CARTA-015
CARTA-016