Cant output results to CSV file

I am new to power shell and I am trying to create a simple script that will check a list of computers and return what icons are on the desktop. I am able to see the results I want on the screen, however when I output to a .csv file it only outputs the results from the first pc in the list and not any or the other PC’s in the list.

My code is below, can you tell me what I am doing wrong?

Get-Content C:\Temp\computers.txt | % {

$ComputerName = $_

Get-Childitem -Path \$ComputerName\path to desktop | Select-Object directoryname, $ComputerName, Name, LastWriteTime, length | Export-Csv -Path c:\Temp\Icons.csv -Encoding ascii -NoTypeInformation

Import-Csv -Path c:\Temp\Icons.csv

}

 

Thanks!

Is it the same user on each PC? Why are you exporting and importing it again? You’re saying that shows you everything you want?

I have to assume it’s not the same user on every computer. See if this does what you’re wanting.

Get-Content C:\Temp\computers.txt | foreach -Begin{
    Function Get-FriendlySize {
        Param($bytes)
        switch($bytes){
            {$_ -gt 1GB}{"{0:N2} GB" -f ($_ / 1GB);break}
            {$_ -gt 1MB}{"{0:N2} MB" -f ($_ / 1MB);break}
            {$_ -gt 1KB}{"{0:N2} KB" -f ($_ / 1KB);break}
            default {"{0:N2} Bytes" -f $_}
        }
    }
}{
    foreach($user in Get-ChildItem "\\$_\c$\users\" -Exclude public -Directory)
    {
        foreach($item in Get-ChildItem "$($user.fullname)\desktop" -file)
        {
            [PSCustomObject]@{
                Computer  = $_
                User      = $user.name
                Item      = $item.name
                Path      = $item.directoryname
                LastWrite = $item.lastwritetime
                Size      = Get-FriendlySize $item.Length
            }
        }
    }
} -OutVariable results

Once you confirm the output shows what you want, you can export it to a single CSV like this.

$results | Export-Csv -Path c:\Temp\Icons.csv -NoTypeInformation

I am using my user id as I am an admin to run the script.

Most systems do not have a user logged on. When a users is logged on there are different users logged on to each system

The import was to be commented out, that was a typo on my part.

I see everything I need on the screen but the script does not output the results for each machine to the CSV file.

It only outputs the results for the first system in the list.

Maybe I am not wording it right. Each user has their own desktop. So Alice on PC1 it will be \pc1\c$\users\alice but what if other users have a profile there? Are you wanting all desktop icons from all profiles on each PC?

Is there a way to limit the script to look for shortcuts only (.lnk) ?

Get-Content C:\Temp\computers.txt | foreach {
    foreach($user in Get-ChildItem "\\$_\c$\users\" -Exclude public -Directory)
    {
        foreach($item in Get-ChildItem "$($user.fullname)\desktop" -file -filter *.lnk)
        {
            [PSCustomObject]@{
                Computer  = $_
                User      = $user.name
                Item      = $item.name
                Path      = $item.directoryname
                LastWrite = $item.lastwritetime
            }
        }
    }
} -OutVariable results

Yes all desktop icons from all profiles on each PC.

But for lab and test systems desktop icons from one specific profile on each PC.

If possible I would like to get a list of shortcuts only and not any files that are on the desktop such as Word, PowerPoint etc.

Yes see my last post.

I did see your last post and the code did work perfectly.

Now I am trying to get the same information from out test and lab systems.

However the icons are on the D drive in a folder called data\profiles

I am using the code below and it does work but I am not sure if this is the most efficient way to do it.

Also is there a way to a blank line in the CSV file to spate the data from each computer

Get-Content C:\Temp\computers.txt | foreach {

foreach($Computer in Get-ChildItem “\$_\D$\data\Profiles” -Exclude public, user -Directory)

{

foreach($item in Get-ChildItem “\$_\D$\data\Profiles\desktop” -file -filter *.lnk)

{

[PSCustomObject]@{

Computer = $_

User = $user.name

Item = $item.name

Path = $item.directoryname

LastWrite = $item.lastwritetime

}

}

}

} -OutVariable results

$results | Export-Csv -Path c:\Temp\Icons.csv -NoTypeInformation

IMO, the best way to add a blank line to a CSV while still maintaining its schema, would be to output a custom object where all properties are empty strings:

[PSCustomObject]@{
    Computer = ""
    User = ""
    Item = ""
    Path = ""
    LastWrite = ""
}

Just add something like that at the end of your $computer foreach loop.