# Output to Excel

**URL:** <https://forums.powershell.org/t/output-to-excel/3700>\
**Category:** PowerShell Help\
**Created:** [February 2, 2015, 6:45am UTC](https://forums.powershell.org/t/output-to-excel/3700 "2015-02-02T06:45:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffrey-ritch](https://avatars.discourse-cdn.com/v4/letter/j/f05b48/32.png) [@jeffrey-ritch](https://forums.powershell.org/u/jeffrey-ritch)\
**Post date:** [February 2, 2015, 6:45am UTC](https://forums.powershell.org/t/output-to-excel/3700/1 "2015-02-02T06:45:49Z")

</div>

I’m trying to create a PS script that takes input from a text file, gets the services running from our servers and exports that to one excel file.  
I have everything working except that as the script creates the excel file, it over rights each of the first line over and over for each server.  
Q: How do I get my script to drop down to the next line and record the data and then drop down again for more data.

# Thank you for any help!

# Get Server Information

param (  
[string]$ServerArray = (Get-Content -Path c:\temp\serverlist.txt)  
)

# Create new Excel workbook

Write-Verbose “$(Get-Date): Script begins!”  
Write-Verbose “$(Get-Date): Opening Excel…”  
$Excel = New-Object -ComObject Excel.Application  
$Excel.Visible = $True  
$Excel = $Excel.Workbooks.Add()  
$Sheet = $Excel.Worksheets.Item(1)  
$Sheet.Name = “Server Services”  
#======================================================  
$Sheet.Cells.Item(1,1) = “Server”  
$Sheet.Cells.Item(1,2) = “Service Name”  
$Sheet.Cells.Item(1,3) = “Status”  
$Sheet.Cells.Item(1,4) = “Startup Type”  
$Sheet.Cells.Item(1,5) = “Service Account”  
#=======================================================  
$intRow = 2  
$WorkBook = $Sheet.UsedRange  
$WorkBook.Interior.ColorIndex = 40  
$WorkBook.Font.ColorIndex = 11  
$WorkBook.Font.Bold = $True  
#=======================================================

# Main loop of script

# ForEach ($Server in $ServerArray) { Get-WmiObject win32\_service -ComputerName $Server | where {$_.startname -notlike “LocalService\*” -and $_.startname -notlike “_local_system\*” -and $_.startname -notlike “NetworkService\*”} | select Name, { If ($Server -notlike “SERVERTEST”) { $Sheet.Cells.Item($intRow, 1) = $_.systemname $Sheet.Cells.Item($intRow, 2) = $_.Name $Sheet.Cells.Item($intRow, 3) = $_.State $Sheet.Cells.Item($intRow, 4) = $_.StartMode $Sheet.Cells.Item($intRow, 5) = $_.StartName } } $WorkBook.EntireColumn.AutoFit() | Out-Null } $intRow ++ $Sheet.Cells.Item($intRow,1) = “Server Inventory Completed” $Sheet.Cells.Item($intRow,1).Font.Bold = $True $Sheet.Cells.Item($intRow,1).Interior.ColorIndex = 40 $Sheet.Cells.Item($intRow,2).Interior.ColorIndex = 40 $Sheet.Cells.Item($intRow,3).Interior.ColorIndex = 40 Write-Verbose “$(Get-Date): Completed!”

---

<div class="post-metadata">

**Author:** ![\_timpringle](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/_timpringle/32/276_2.png) [@\_timpringle](https://forums.powershell.org/u/_timpringle)\
**Post date:** [February 2, 2015, 7:23am UTC](https://forums.powershell.org/t/output-to-excel/3700/2 "2015-02-02T07:23:57Z")

</div>

Hey Jeffrey,

Your command for incrementing the row is outside of the For…Each loop, which is causing this.

If you place the

```
$intRow++
```
 immediately after the line with 
```
$WorkBook.EntireColumn.AutoFit() | Out-Null
```
, you should be okay.

---

<div class="post-metadata">

**Author:** ![jeffrey-ritch](https://avatars.discourse-cdn.com/v4/letter/j/f05b48/32.png) [@jeffrey-ritch](https://forums.powershell.org/u/jeffrey-ritch)\
**Post date:** [February 2, 2015, 7:43am UTC](https://forums.powershell.org/t/output-to-excel/3700/3 "2015-02-02T07:43:48Z")

</div>

Tim,  
Thank you for your reply.  
That places the output for each server on a new line (correct) but doesn’t lable it on as a server name. Then the same responce as before for each sever that has an output, it over rights it on that server line in excell. Not dropping down to the next.  
Jeffrey

---

<div class="post-metadata">

**Author:** ![jeffrey-ritch](https://avatars.discourse-cdn.com/v4/letter/j/f05b48/32.png) [@jeffrey-ritch](https://forums.powershell.org/u/jeffrey-ritch)\
**Post date:** [February 2, 2015, 12:59pm UTC](https://forums.powershell.org/t/output-to-excel/3700/4 "2015-02-02T12:59:07Z")

</div>

With a little luck, I got this working. I just need one more item. On the output, if a server doesn’t have any output, it just drops a line and continues to the next server. Which is fine, except I would like a list of all servers, even if there isn’t any services that need reported.

If anyone has any ideas?

# ====================================================== Start of the script

#======================================================

# Get Server Information

# Modify the path to get list of servers

#======================================================  
[CmdletBinding()]  
param (  
[string]$ServerArray = (Get-Content -Path c:\temp\listservers.txt)  
)  
#======================================================

# Create new Excel workbook

#======================================================  
Write-Verbose “$(Get-Date): Script begins!”  
Write-Verbose “$(Get-Date): Opening Excel…”  
$Excel = New-Object -ComObject Excel.Application  
$Excel.Visible = $True  
$Excel = $Excel.Workbooks.Add()  
$Sheet = $Excel.Worksheets.Item(1)  
$Sheet.Name = “Server Services”  
#======================================================

# Place Column Header Names in Excel file

#======================================================  
$Sheet.Cells.Item(1,1) = “Server”  
$Sheet.Cells.Item(1,2) = “Service Name”  
$Sheet.Cells.Item(1,3) = “Status”  
$Sheet.Cells.Item(1,4) = “Startup Type”  
$Sheet.Cells.Item(1,5) = “Service Account”  
#=======================================================

# Format the Top Header Rows

#=======================================================  
$intRow = 2  
$WorkBook = $Sheet.UsedRange  
$WorkBook.Interior.ColorIndex = 40  
$WorkBook.Font.ColorIndex = 11  
$WorkBook.Font.Bold = $True  
#=======================================================

# Main Loop

# Gets the computer name from the txt file, searches and removes any notlike

# Places results in the Excel file

#=======================================================  
ForEach ($Server in $ServerArray)  
{  
$datainfo = Get-WmiObject win32\_service -ComputerName $Server | where {$_.startname -notlike “LocalService\*” -and $_.startname -notlike “_local_system\*” -and $_.startname -notlike “NetworkService\*”}  
$datainfo | % {  
If ($Server -notlike “SERVERTEST”)  
{ $Sheet.Cells.Item($intRow, 1) = $_.systemname  
$Sheet.Cells.Item($intRow, 2) = $_.Name  
$Sheet.Cells.Item($intRow, 3) = $_.State  
$Sheet.Cells.Item($intRow, 4) = $_.StartMode  
$Sheet.Cells.Item($intRow, 5) = $_.StartName  
$intRow ++  
}  
$WorkBook.EntireColumn.AutoFit() | Out-Null  
}  
}  
#=======================================================

# Format the Ending Row

#=======================================================  
$intRow ++  
$Sheet.Cells.Item($intRow,1) = “Server Inventory Completed”  
$Sheet.Cells.Item($intRow,1).Font.Bold = $True  
$Sheet.Cells.Item($intRow,1).Interior.ColorIndex = 40  
$Sheet.Cells.Item($intRow,2).Interior.ColorIndex = 40  
$Sheet.Cells.Item($intRow,3).Interior.ColorIndex = 40  
Write-Verbose “$(Get-Date): Completed!”

# ====================================================== End of the script

---

<div class="post-metadata">

**Author:** ![jeffrey-ritch](https://avatars.discourse-cdn.com/v4/letter/j/f05b48/32.png) [@jeffrey-ritch](https://forums.powershell.org/u/jeffrey-ritch)\
**Post date:** [February 16, 2015, 9:10am UTC](https://forums.powershell.org/t/output-to-excel/3700/5 "2015-02-16T09:10:50Z")

</div>

This should be the finished product. Copy and make changes as needed.

#======================================================

# Get Server Information

# Modify the path to get list of servers

#======================================================  
param (  
[string]$ServerArray = (Get-Content -Path ListServers.txt)  
)  
#======================================================

# Create new Excel workbook

#======================================================  
Write-Verbose “$(Get-Date): Script begins!”  
Write-Verbose “$(Get-Date): Opening Excel…”  
$Excel = New-Object -ComObject Excel.Application  
$Excel.Visible = $True  
$Excel = $Excel.Workbooks.Add()  
$Sheet = $Excel.Worksheets.Item(1)  
$Sheet.Name = “Server Services”  
#=======================================================

# Place Column Header Names in Excel file

#=======================================================  
$Sheet.Cells.Item(1,1) = “Server”  
$Sheet.Cells.Item(1,2) = “Service Name”  
$Sheet.Cells.Item(1,3) = “Status”  
$Sheet.Cells.Item(1,4) = “Startup Type”  
$Sheet.Cells.Item(1,5) = “Service Account”  
#=======================================================

# Format the Top Header Rows

#=======================================================  
$intRow = 2  
$WorkBook = $Sheet.UsedRange  
$WorkBook.Interior.ColorIndex = 40  
$WorkBook.Font.ColorIndex = 11  
$WorkBook.Font.Bold = $True  
#=======================================================

# Main Loop

# Gets the computer name from the txt file, searches and removes any notlike

# Places results in the Excel file

#=======================================================  
ForEach ($Server in $ServerArray)  
{  
$Sheet.Cells.Item($intRow, 1) = $Server  
$Sheet.Cells.Item($intRow, 2) = “”  
$Sheet.Cells.Item($intRow, 3) = “”  
$Sheet.Cells.Item($intRow, 4) = “”  
$Sheet.Cells.Item($intRow, 5) = “”  
$a=Get-WmiObject win32\_service -ComputerName $Server | where {$_.startname -notlike “LocalService\*” -and $_.startname -notlike “_local_system\*” -and $\_.startname -notlike “_Network_Service\*”}  
$a | % {  
If ($Server -notlike “SERVERTEST”)  
{

```
      If ($Server -ne $_.systemname)
      {
      $intRow ++
      }
      else
      {
      $Sheet.Cells.Item($intRow, 1) = $_.systemname
        $Sheet.Cells.Item($intRow, 2) = $_.Name
        $Sheet.Cells.Item($intRow, 3) = $_.State
        $Sheet.Cells.Item($intRow, 4) = $_.StartMode
        $Sheet.Cells.Item($intRow, 5) = $_.StartName
      $intRow ++
      }
    }   
	$WorkBook.EntireColumn.AutoFit() | Out-Null

```

}  
}  
#=======================================================

# Format the Ending Row

#=======================================================  
$intRow ++  
$Sheet.Cells.Item($intRow,1) = “Server Inventory Completed”  
$Sheet.Cells.Item($intRow,1).Font.Bold = $True  
$Sheet.Cells.Item($intRow,1).Interior.ColorIndex = 40  
$Sheet.Cells.Item($intRow,2).Interior.ColorIndex = 40  
$Sheet.Cells.Item($intRow,3).Interior.ColorIndex = 40  
Write-Verbose “$(Get-Date): Completed!”

---

<div class="post-metadata">

**Author:** ![martin-nielsen](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@martin-nielsen](https://forums.powershell.org/u/martin-nielsen)\
**Post date:** [February 19, 2015, 1:23am UTC](https://forums.powershell.org/t/output-to-excel/3700/6 "2015-02-19T01:23:47Z")

</div>

```
Get-Service | Export-Csv -UseCulture -NoTypeInformation services.csv
```

This’ll open up just fine in Excel. Granted it doesn’t have colors, but it can run on any machine, even without Excel installed.

To get a list of services from different machines, you could do

```
foreach($computer in $computers) { 
    Get-Service -ComputerName $computer | Add-Member -NotePropertyName Computer -NotePropertyValue $computer -PassThru | Export-Csv -Append -UseCulture -NoTypeInformation services.csv 
}
```

---

<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:46pm UTC](https://forums.powershell.org/t/output-to-excel/3700/7 "2024-05-16T20:46:19Z")

</div>

Closed on May 16, 2024, 8:46pm UTC


