Cant output results to CSV file

Welcome Forums General PowerShell Q&A Cant output results to CSV file

Viewing 10 reply threads
  • Author
    Posts
    • #257435
      Participant
      Topics: 1
      Replies: 4
      Points: 22
      Rank: Member

      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!

      • This topic was modified 1 month, 1 week ago by mzimme229.
    • #257444
      Participant
      Topics: 8
      Replies: 617
      Points: 2,393
      Helping Hand
      Rank: Community Hero

      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?

    • #257447
      Participant
      Topics: 8
      Replies: 617
      Points: 2,393
      Helping Hand
      Rank: Community Hero

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

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

    • #257450
      Participant
      Topics: 1
      Replies: 4
      Points: 22
      Rank: Member

      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.

    • #257453
      Participant
      Topics: 8
      Replies: 617
      Points: 2,393
      Helping Hand
      Rank: Community Hero

      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?

    • #257456
      Participant
      Topics: 1
      Replies: 4
      Points: 22
      Rank: Member

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

    • #257462
      Participant
      Topics: 8
      Replies: 617
      Points: 2,393
      Helping Hand
      Rank: Community Hero
    • #257465
      Participant
      Topics: 1
      Replies: 4
      Points: 22
      Rank: Member

      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.

    • #257477
      Participant
      Topics: 8
      Replies: 617
      Points: 2,393
      Helping Hand
      Rank: Community Hero

      Yes see my last post.

    • #257561
      Participant
      Topics: 1
      Replies: 4
      Points: 22
      Rank: Member

      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

    • #257651
      Participant
      Topics: 1
      Replies: 85
      Points: 387
      Helping Hand
      Rank: Contributor

      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:

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

Viewing 10 reply threads
  • You must be logged in to reply to this topic.