2

I am pretty new to powershell and have been trying various ways to split the results from the following powershell script I found but have had no luck. At the moment the script list everything in 4 columns with no gap in between the results of each server.

(when it finishes with the results for one server I would like a gap or even a header with the server name between each result to make it easier to read when run against multiple servers)

any help to get this formatted would be appreciated.

Get-WmiObject -ComputerName ("server1","server2") Win32_UserProfile | % {
  $userinfo = [WMI] ($_.__Path -Replace "Win32_UserProfile", "Win32_SID")
  New-Object PsObject -Property @{
    ComputerName= $_.__Server 
    Domain      = $userinfo.ReferencedDomainName
    User        = $userinfo.AccountName
    LastUsed    = $_.ConvertToDatetime($_.LastUseTime)

  }
} | export-csv -NoType c:\Results.csv

1 Answer 1

1

I'm going to assume that when you say you want "easier to read" output, you mean want easier to read output on the console, and not in that CSV file. Because CSV files are not meant to be pretty or easy for humans to read - CSV files are meant to be easy for computers to process.

So to that end, I've just ignored the Export-CSV cmdlet at the end. In my opinion, getting nice console output and getting efficient CSV data are two totally separate goals.

When I run your script, the output looks like this:

User            ComputerName    Domain          LastUsed             
----            ------------    ------          --------             
Ryan            SERVER1         SERVER1         9/25/2014 11:53:54 PM
NETWORK SERVICE SERVER1         NT AUTHORITY    9/25/2014 11:53:54 PM
LOCAL SERVICE   SERVER1         NT AUTHORITY    9/25/2014 11:53:54 PM
SYSTEM          SERVER1         NT AUTHORITY    9/25/2014 11:53:54 PM
Ryan            SERVER2         SERVER2         9/25/2014 11:53:54 PM
NETWORK SERVICE SERVER2         NT AUTHORITY    9/25/2014 11:53:54 PM
LOCAL SERVICE   SERVER2         NT AUTHORITY    9/25/2014 11:53:54 PM
SYSTEM          SERVER2         NT AUTHORITY    9/25/2014 11:53:54 PM

I see what you mean about the console not separating out or putting any "gap" between each computer.

So I made some modifications to your script:

:NextComputer Foreach ($Computer In "localhost", "localhost")
{
    Write-Host "                                      " -ForegroundColor Green -BackgroundColor Black
    Write-Host " Massive Flashy Header For $Computer! " -ForegroundColor Green -BackgroundColor Black
    Try
    {
        $Profiles = Get-WMIObject -ClassName Win32_UserProfile -ComputerName $Computer -ErrorAction Stop
    }
    Catch
    {
        Write-Error "Error while getting user profiles from $Computer`: $($_.Exception.Message)"
        Continue NextComputer
    }    
    Write-Host " User profiles found: $($Profiles.Count)               " -ForegroundColor Green -BackgroundColor Black
    If ($Profiles.Count -LT 1)
    {
        Continue NextComputer
    }
    $ProfileCollection = @()
    :NextProfile Foreach ($Profile In $Profiles)
    {
        $UserInfo = [WMI] ($Profile.__PATH -Replace "Win32_UserProfile", "Win32_SID")
        $ProfileCollection += New-Object PsObject -Property @{
            ComputerName = $Computer 
            Domain       = $UserInfo.ReferencedDomainName
            User         = $UserInfo.AccountName
            LastUsed     = $Profile.ConvertToDatetime($Profile.LastUseTime)
        }
    }
    $ProfileCollection | Format-Table -AutoSize
}

And now the output looks a little more readable, since the results from each computer are now separated from one another:

 Massive Flashy Header For SERVER1! 
 User profiles found: 4               

User            ComputerName Domain          LastUsed             
----            ------------ ------          --------             
Ryan            SERVER1      SERVER1         9/25/2014 11:56:50 PM
NETWORK SERVICE SERVER1      NT AUTHORITY    9/25/2014 11:56:50 PM
LOCAL SERVICE   SERVER1      NT AUTHORITY    9/25/2014 11:56:50 PM
SYSTEM          SERVER1      NT AUTHORITY    9/25/2014 11:56:50 PM



 Massive Flashy Header For SERVER2! 
 User profiles found: 4               

User            ComputerName Domain          LastUsed             
----            ------------ ------          --------             
Ryan            SERVER2      SERVER2         9/25/2014 11:56:50 PM
NETWORK SERVICE SERVER2      NT AUTHORITY    9/25/2014 11:56:50 PM
LOCAL SERVICE   SERVER2      NT AUTHORITY    9/25/2014 11:56:50 PM
SYSTEM          SERVER2      NT AUTHORITY    9/25/2014 11:56:50 PM

More to your liking, yes?

2
  • yeah that looks really good - I will need to export this into something so since you said csv is not good do you have any suggestions on what I can export this to? HTML? xls? can this be exported?
    – MJT
    Sep 26, 2014 at 22:22
  • CSV is fine to store this kind of data in, if I were you I'd be fine with storing the output from your original script in a CSV just like it was. If you want to make something that is pretty and easy on the eyes, then yes HTML is the way to go.
    – Ryan Ries
    Sep 26, 2014 at 22:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .