header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
formatting data into excel powershell
Last Post 20 Sep 2010 02:18 PM by George Howarth. 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
thutmoseUser is Offline
New Member
New Member
Posts:2
Avatar

--
19 Sep 2010 06:28 PM
    Hello guys I am a newbie here that is loving powershell and thankful for a site like this. I have this script here and here is what I am trying to accomplish. The script below works great with the output below.I would like to sharpen it up a bit for readibility purposes. Please read my comments as they are interwoven.


    This is the script below

    $TotalCount = @{} Get-ChildItem -Recurse \\servername\share | foreach { $objOwner = (Get-ACL $_.FullName).Owner
    $Totalcount[$objOwner] += $_.Length }
    $TotalCount | Format-wide

    Here is the output is below
    user size
    user1 1254741
    user2 145221
    user3 95412477
    user4 554412
    user5 2144110
    user6 554712
    user7 11441

    I am now trying to get the output to save in an excel file instead.

    $TotalCount = @{} Get-ChildItem -Recurse \\servername\share | foreach { $objOwner = (Get-ACL $_.FullName).Owner $Totalcount[$objOwner] += $_.Length } $TotalCount | Format-wide | export-csv c:\users\svc_xch\log.xls

    My goal is to get the output to work like this in excel. I would like to have the GB for gigabytes and MB for megabytes. Any clues please.

    USER SIZE userA 125GB
    USERB 140MB
    USERC 320GB
    USERD 73MB
    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    19 Sep 2010 06:33 PM
    I won't provide you with a full answer, but a few things:
    1. You will need to end with something like this:
    PS> $TotalCount|Export-Csv c:\users\svc_xch\log.csv

    2. See that I've stuck with .csv above. You will need to use "Open" and import this, then save to XLS/XLSX.

    There are other ways to interact with Excel, but they are generally slower if that could be an issue...
    thutmoseUser is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    20 Sep 2010 02:53 AM
    I really do not think you read my question. Thank you for attempting to help though.
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    20 Sep 2010 02:18 PM

    Export-CSV is for CSV files and you can open those file types with Excel. If you want to go straight from raw CSV data to an Excel spreadsheet then that is an entirely different ball game altogether. For example (taken from http://poshtips.com/2009/12/02/use-...mi-data/):

    #
    # Wmi-to-Excel.ps1 - 12/02/2009 by Kahuna at http://PoshTips.com
    #

    $erroractionpreference = "SilentlyContinue"
    $a = New-Object -ComObject Excel.Application
    $a.visible = $True
    $b = $a.Workbooks.Add()
    $c = $b.Worksheets.Item(1)

    #define column headers for the first row of the spreadsheet
    $ColHeaders = @(
        "Organization",
        "Server Name",
        "Operating System",
        "Service Packs",
        "System Type",
        "Install Date",
        "Manufacturer",
        "Model",
        "Service Tag",
        "Serial Number",
        "Number of Processors",
        "Total Physical Memory (GB)",
        "Last Reboot Time",
        "Report TimeStamp"
    )

    $idx = 0

    # write the column headings to the spreadsheet
    foreach ($title in $ColHeaders)
    {
        $idx += 1
        $c.Cells.Item(1, $idx) = $title
    }

    $d = $c.UsedRange
    $d.Interior.ColorIndex = 19
    $d.Font.ColorIndex = 11
    $d.Font.Bold = $True
    $intRow = 2
    #get contents of file (passed in $args[0]) containing a host list
    foreach ($strComputer in get-content $args[0])
    {
        write-host "Processing $strComputer..."
        OS    = gwmi -computername $strComputer Win32_OperatingSystem
        $Computer = gwmi -computername $strComputer Win32_computerSystem
        $Bios  = gwmi -computername $strComputer win32_bios
       
        # populate each row of the spreadsheet with data collected from WMI
        $c.Cells.Item($intRow,1) = $OS.Organization
        $c.Cells.Item($intRow,2) = $strComputer.Toupper()
        $c.Cells.Item($intRow,3) = $OS.Caption
        $c.Cells.Item($intRow,4) = $OS.CSDVersion
        $c.Cells.Item($intRow,5) = $Computer.SystemType
        $c.Cells.Item($intRow,6) = [System.Management.ManagementDateTimeconverter]::ToDateTime($OS.InstallDate)
        $c.Cells.Item($intRow,7) = $Computer.Manufacturer
        $c.Cells.Item($intRow,8) = $Computer.Model
        $c.Cells.Item($intRow,9) = $Bios.serialnumber
        $c.Cells.Item($intRow,10) = $OS.SerialNumber
        $c.Cells.Item($intRow,11) = $Computer.NumberOfProcessors
        $c.Cells.Item($intRow,12) = "{0:N0}" -f ($computer.TotalPhysicalMemory/1GB)
        $c.Cells.Item($intRow,13) = [System.Management.ManagementDateTimeconverter]::ToDateTime($OS.LastBootUpTime)
        $c.Cells.Item($intRow,14) = Get-date
        $intRow += 1
    }

    # resize the columns to fit the data
    $d.EntireColumn.AutoFit() | Out-Null

    Also, I don't quite understand your last statement:

    My goal is to get the output to work like this in excel. I would like to have the GB for gigabytes and MB for megabytes. Any clues please.


    You want to convert some raw values into GB/MB?

    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer