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

**URL:** <https://forums.powershell.org/t/adding-a-string-to-a-list-returns-the-length-of-the-string/18357>\
**Category:** PowerShell Help\
**Created:** [January 14, 2022, 10:13pm UTC](https://forums.powershell.org/t/adding-a-string-to-a-list-returns-the-length-of-the-string/18357 "2022-01-14T22:13:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelkerrick](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@michaelkerrick](https://forums.powershell.org/u/michaelkerrick)\
**Post date:** [January 14, 2022, 10:13pm UTC](https://forums.powershell.org/t/adding-a-string-to-a-list-returns-the-length-of-the-string/18357/1 "2022-01-14T22:13:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 14, 2022, 10:22pm UTC](https://forums.powershell.org/t/adding-a-string-to-a-list-returns-the-length-of-the-string/18357/2 "2022-01-14T22:22:50Z")

</div>

Michael,  
Welcome to the forum. 👋🏽

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.

```auto
$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

```

---

<div class="post-metadata">

**Author:** ![michaelkerrick](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@michaelkerrick](https://forums.powershell.org/u/michaelkerrick)\
**Post date:** [January 24, 2022, 11:28pm UTC](https://forums.powershell.org/t/adding-a-string-to-a-list-returns-the-length-of-the-string/18357/3 "2022-01-24T23:28:57Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 24, 2022, 11:34pm UTC](https://forums.powershell.org/t/adding-a-string-to-a-list-returns-the-length-of-the-string/18357/4 "2022-01-24T23:34:41Z")

</div>

Does the directory creation and file copy part work?

---

<div class="post-metadata">

**Author:** ![michaelkerrick](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@michaelkerrick](https://forums.powershell.org/u/michaelkerrick)\
**Post date:** [January 24, 2022, 11:50pm UTC](https://forums.powershell.org/t/adding-a-string-to-a-list-returns-the-length-of-the-string/18357/5 "2022-01-24T23:50:40Z")

</div>

Yes that part works.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [January 24, 2022, 11:54pm UTC](https://forums.powershell.org/t/adding-a-string-to-a-list-returns-the-length-of-the-string/18357/6 "2022-01-24T23:54:29Z")

</div>

Add a header to that export

---

<div class="post-metadata">

**Author:** ![WinOpsEngineer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/winopsengineer/32/2029_2.png) [@WinOpsEngineer](https://forums.powershell.org/u/WinOpsEngineer)\
**Post date:** [January 25, 2022, 4:19pm UTC](https://forums.powershell.org/t/adding-a-string-to-a-list-returns-the-length-of-the-string/18357/7 "2022-01-25T16:19:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michaelkerrick](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@michaelkerrick](https://forums.powershell.org/u/michaelkerrick)\
**Post date:** [January 25, 2022, 8:29pm UTC](https://forums.powershell.org/t/adding-a-string-to-a-list-returns-the-length-of-the-string/18357/8 "2022-01-25T20:29:42Z")

</div>

Here you go.

(Attachment StaffComputers.txt is missing)

---

<div class="post-metadata">

**Author:** ![michaelkerrick](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@michaelkerrick](https://forums.powershell.org/u/michaelkerrick)\
**Post date:** [January 25, 2022, 8:30pm UTC](https://forums.powershell.org/t/adding-a-string-to-a-list-returns-the-length-of-the-string/18357/9 "2022-01-25T20:30:58Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![michaelkerrick](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@michaelkerrick](https://forums.powershell.org/u/michaelkerrick)\
**Post date:** [January 25, 2022, 8:37pm UTC](https://forums.powershell.org/t/adding-a-string-to-a-list-returns-the-length-of-the-string/18357/10 "2022-01-25T20:37:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![michaelkerrick](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@michaelkerrick](https://forums.powershell.org/u/michaelkerrick)\
**Post date:** [March 7, 2022, 10:19pm UTC](https://forums.powershell.org/t/adding-a-string-to-a-list-returns-the-length-of-the-string/18357/11 "2022-03-07T22:19:54Z")

</div>

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  
…

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:18pm UTC](https://forums.powershell.org/t/adding-a-string-to-a-list-returns-the-length-of-the-string/18357/12 "2024-05-16T20:18:10Z")

</div>


