# using get-content from csv file, but wanting to format to word table

**URL:** <https://forums.powershell.org/t/using-get-content-from-csv-file-but-wanting-to-format-to-word-table/11107>\
**Category:** PowerShell Help\
**Created:** [August 9, 2018, 4:06pm UTC](https://forums.powershell.org/t/using-get-content-from-csv-file-but-wanting-to-format-to-word-table/11107 "2018-08-09T16:06:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonleech](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jasonleech](https://forums.powershell.org/u/jasonleech)\
**Post date:** [August 9, 2018, 4:06pm UTC](https://forums.powershell.org/t/using-get-content-from-csv-file-but-wanting-to-format-to-word-table/11107/1 "2018-08-09T16:06:22Z")

</div>

Hi all,

The companyn I work for has a service sheet that is in Word, with loads of tables (just how it’s done)…my issue is, I can create tables from Powershell to Word, but can’t see a way around using get-content (which has no way to format the contents) whih is causing me problems…i would like to output the get-content into 1 cell, but formatted in column lines…Is ther another way to do ths, that I’m missing? Any help greatly appreciated…

```
# Disk Info:
$CSV = "C:\Test\Disk.csv"
$Disk =(Get-WmiObject -Class Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3} |
 Sort-Object -Property Name | 
    Select-Object Name, 
        @{"Label"=" Size(GB)";"Expression"={"{0:N}" -f ($_.Size/1GB) -as [float]}}, 
        @{"Label"=" Free(GB)";"Expression"={"{0:N}" -f ($_.FreeSpace/1GB) -as [float]}}, 
        @{"Label"=" %Free";"Expression"={"{0:N}" -f ($_.FreeSpace/$_.Size*100) -as [float]}} | Export-CSV -delimiter “,” $CSV -noTypeInformation)

(Get-Content C:\Test\Disk.csv) | % {$_ -replace '"', ""} | out-file -FilePath C:\Test\Disk.csv -Force -Encoding ascii
(Get-Content C:\Test\Disk.csv) | % {$_ -replace ',', " "} | out-file -FilePath C:\Test\Disk.csv -Force -Encoding ascii
$Info = (Get-Content $CSV) 
$word = New-Object -ComObject word.application
$word.visible = $true
$word.application.DisplayAlerts = 0
$doc = $word.documents.add()
$selection = $word.selection
$selection.Font.Name = "Arial"
$selection.Font.Size = 14
$selection.Font.Bold = $true;
$selection.Font.Color = 255
$selection.typeText("This is a Test")
$selection.paragraphFormat.Alignment = 1
$selection.TypeParagraph()
$selection.Font.Name = "Arial"
$selection.Font.Size = 10
$selection.Font.Color = 1
$selection.paragraphFormat.Alignment = 2
$selection.Font.Bold = $false;
$selection.TypeText($fDATE);$selection.TypeParagraph()
$selection.Font.Name = "Arial"
$selection.Font.Size = 11
$selection.paragraphFormat.Alignment = 1
$selection.TypeParagraph()
$number_Of_Rows = 3;$number_Of_Columns = 3
$paragraph = $doc.Content.Paragraphs.Add()
$Range = $paragraph.Range
$Table = $doc.Tables.Add($Range,$number_Of_Rows,$number_Of_Columns)
$Table = $doc.Tables.item(1)
$Table.Borders.OutsideLineStyle = 1
$Table.Borders.InsideLineStyle = 1
$Table.shading.BackgroundPatternColor = $NTS
$Table.Cell(1,1).Range.Font.Bold = $True
$Table.Cell(1,1).Range.Font.Italic = $True
$Table.Cell(1,1).Range.Font.Name =”Calibri”
#$Table.Cell(1,1).Range.shading.BackgroundPatternColor = $NTS
#$Table.Cell(1,3).Range.shading.BackgroundPatternColor = $NTS
$Table.Cell(1,3).Range.Font.Name =”Calibri”
$Table.Cell(1,2).Range.Font.Bold = $True
$Table.Cell(1,3).Range.Font.Bold = $True
$Table.Cell(1,1).PreferredWidthType = 3
$Table.Cell(1,1).Width = 50
$Table.Cell(1,2).PreferredWidthType = 3
$Table.Cell(1,2).Width = 75
$Table.Cell(1,3).PreferredWidthType = 3
$Table.Cell(1,3).Width = 200
$Table.Cell(2,1).PreferredWidthType = 3
$Table.Cell(2,1).Width = 50
$Table.Cell(2,2).PreferredWidthType = 3
$Table.Cell(2,2).Width = 75
$Table.Cell(2,3).PreferredWidthType = 3
$Table.Cell(2,3).Width = 200
$Table.Cell(3,1).PreferredWidthType = 3
$Table.Cell(3,1).Width = 50
$Table.Cell(3,2).PreferredWidthType = 3
$Table.Cell(3,2).Width = 75
$Table.Cell(3,3).PreferredWidthType = 3
$Table.Cell(3,3).Width = 200
$Table.Cell(1,1).Range.Text = "$Date"
$Table.Cell(1,2).Range.Text = "COL 2"
$Table.Cell(1,3).Range.Text = "$Info"
$Table.Cell(1,3).Range.Font.Name = "$Arial"
$Table.Cell(1,3).Range.Font.Size = 10
$selection.Font.Bold = $false;
$Table.Cell(2,1).Range.Text = "A Value"
$Table.Cell(2,2).Range.Text = "B Value"
$Table.Cell(2,3).Range.Text = "C Value"
$Table.Cell(3,1).Range.Text = "D Value"
$Table.Cell(3,2).Range.Text = "E Value"
$Table.Cell(3,3).Range.Text = "F Value"
$selection.EndKey(6)
$selection.TypeParagraph()
```

As you can see, I’ve tried ‘formatting’ spaces to achieve the results, but a bit flakey to be honest!  
I would look into Excel output, to create the tables, then transfer to Word, but haven’t had any experience with Excel and Powershell - yet!

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [August 9, 2018, 4:08pm UTC](https://forums.powershell.org/t/using-get-content-from-csv-file-but-wanting-to-format-to-word-table/11107/2 "2018-08-09T16:08:12Z")

</div>

Would it not be easier to use Import-CSV, so you’re getting structured data to work with in the first place? As it is, it’s just all text-munging.

---

<div class="post-metadata">

**Author:** ![jasonleech](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jasonleech](https://forums.powershell.org/u/jasonleech)\
**Post date:** [August 9, 2018, 4:12pm UTC](https://forums.powershell.org/t/using-get-content-from-csv-file-but-wanting-to-format-to-word-table/11107/3 "2018-08-09T16:12:25Z")

</div>

Hi Don, thanks for the quick response! I tried Import-CSV, but it either returns the CSV location, or outputs nothing to the table cell - which I couldn’t work out why! I added -Header too, for Name, etc, but that didn’t work for me! Just learning with the output to Word tables too!

---

<div class="post-metadata">

**Author:** ![jasonleech](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jasonleech](https://forums.powershell.org/u/jasonleech)\
**Post date:** [August 9, 2018, 4:14pm UTC](https://forums.powershell.org/t/using-get-content-from-csv-file-but-wanting-to-format-to-word-table/11107/4 "2018-08-09T16:14:03Z")

</div>

oh, and great new look to the website!

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [August 9, 2018, 4:15pm UTC](https://forums.powershell.org/t/using-get-content-from-csv-file-but-wanting-to-format-to-word-table/11107/5 "2018-08-09T16:15:42Z")

</div>

Well, so, Import-CSV isn’t just bringing in a wedge of text.

```
$rows = Import-CSV whatever.csv

ForEach ($row in $rows) {

$row.column1 # accessing the contents of "column1"

$Table.Cell(2,1).Range.Text = $row.column1

$Table.Cell(2,2).Range.Text = $row.fred

$Table.Cell(2,3).Range.Text = $row.ginger

}
```

&nbsp;

Assuming your CSV has headers named “Column1,” “Fred,” and “Ginger.” In terms of doing anything programmatic in Word, I’m not your guy. That stuff is of the devil. If I need to make reports, I stick the data in a SQL Server table and make the report in SQL Server Reporting Services, which has a drag n drop report designer. Then I schedule the report to run, and schedule scripts to populate the table. Even wrote a book about it :).

---

<div class="post-metadata">

**Author:** ![jasonleech](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jasonleech](https://forums.powershell.org/u/jasonleech)\
**Post date:** [August 9, 2018, 4:22pm UTC](https://forums.powershell.org/t/using-get-content-from-csv-file-but-wanting-to-format-to-word-table/11107/6 "2018-08-09T16:22:44Z")

</div>

Thanks Don, that approach certainly looks tidier, and makes data transferable in a better way. I’ll investigate further - all aprt of the learning curve!

Good idea with SQL - just a shame I haven’t got access to that myself!

&nbsp;

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [August 9, 2018, 4:23pm UTC](https://forums.powershell.org/t/using-get-content-from-csv-file-but-wanting-to-format-to-word-table/11107/7 "2018-08-09T16:23:37Z")

</div>

SQL Express is free :). It’s what I used in the book. Which is also free.

---

<div class="post-metadata">

**Author:** ![jasonleech](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jasonleech](https://forums.powershell.org/u/jasonleech)\
**Post date:** [August 9, 2018, 5:14pm UTC](https://forums.powershell.org/t/using-get-content-from-csv-file-but-wanting-to-format-to-word-table/11107/8 "2018-08-09T17:14:10Z")

</div>

Normal  
0

false  
false  
false

EN-GB  
X-NONE  
X-NONE

/\* Style Definitions \*/  
table.MsoNormalTable  
{mso-style-name:“Table Normal”;  
mso-tstyle-rowband-size:0;  
mso-tstyle-colband-size:0;  
mso-style-noshow:yes;  
mso-style-priority:99;  
mso-style-parent:“”;  
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;  
mso-para-margin-top:0cm;  
mso-para-margin-right:0cm;  
mso-para-margin-bottom:8.0pt;  
mso-para-margin-left:0cm;  
line-height:107%;  
mso-pagination:widow-orphan;  
font-size:11.0pt;  
font-family:“Calibri”,sans-serif;  
mso-ascii-font-family:Calibri;  
mso-ascii-theme-font:minor-latin;  
mso-hansi-font-family:Calibri;  
mso-hansi-theme-font:minor-latin;  
mso-bidi-font-family:“Times New Roman”;  
mso-bidi-theme-font:minor-bidi;  
mso-fareast-language:EN-US;}

Name

Size(GB)

Free(GB)

%Free

C:

222.92

156.35

70.14

D:

14.32

1.72

12.03

```
# General settings
CLS
$fDATE = Get-Date -Format "dddd, MMMM, dd, yyyy"
$Date = ((Get-Date).ToString('dd-MM-yyyy'))
#Colour Settings
$Red = 251
$Green = 212
$Blue = 180
$NTS = [long]($Red + ($Green * 256) + ($Blue * 65536))
#Table cell width settings
$TC1 = 40
$TC2 = 60
$TC3 = 75
$TC4 =75
# Disk Info:
$CSV = "C:\Test\Disk.csv"
$Disk =(Get-WmiObject -Class Win32_LogicalDisk |
Where-Object {$_.DriveType -eq 3} |
Sort-Object -Property Name | 
Select-Object Name, `
@{"Label"="Size(GB)";"Expression"={"{0:N}" -f ($_.Size/1GB) -as [float]}}, `
@{"Label"="Free(GB)";"Expression"={"{0:N}" -f ($_.FreeSpace/1GB) -as [float]}}, `
@{"Label"="%Free";"Expression"={"{0:N}" -f ($_.FreeSpace/$_.Size*100) -as [float]}} | Export-CSV-delimiter“,” $CSV -noTypeInformation)
#(Get-Content C:\Test\Disk.csv) | % {$_ -replace '"', ""} | out-file -FilePath C:\Test\Disk.csv -Force -Encoding ascii
#(Get-Content C:\Test\Disk.csv) | % {$_ -replace ',', " \ "} | out-file -FilePath C:\Test\Disk.csv -Force -Encoding ascii
#$Info = (Get-Content $CSV) 
$word = New-Object -ComObject word.application
$word.visible = $true
$word.application.DisplayAlerts = 0
$doc = $word.documents.add()
$selection = $word.selection
$selection.Font.Name = "Arial"
$selection.Font.Size = 14
$selection.Font.Bold = $true;
$selection.Font.Color = 255
$selection.typeText("This is a Test")
$selection.paragraphFormat.Alignment = 1
$selection.TypeParagraph()
$selection.Font.Name = "Arial"
$selection.Font.Size = 10
$selection.Font.Color = 1
$selection.paragraphFormat.Alignment = 2
$selection.Font.Bold = $false;
$selection.TypeText($fDATE);$selection.TypeParagraph()
$selection.Font.Name = "Arial"
$selection.Font.Size = 11
$selection.paragraphFormat.Alignment = 1
$selection.TypeParagraph()
$number_Of_Rows = 3;$number_Of_Columns = 4
$paragraph = $doc.Content.Paragraphs.Add()
$Range = $paragraph.Range
$Table = $doc.Tables.Add($Range,$number_Of_Rows,$number_Of_Columns)
$Table = $doc.Tables.item(1)
$Table.Borders.OutsideLineStyle = 1
$Table.Borders.InsideLineStyle = 1
$Table.shading.BackgroundPatternColor = $NTS
$Table.Cell(1,1).Range.Font.Bold = $True
$Table.Cell(1,1).Range.Font.Italic = $True
$Table.Cell(1,1).Range.Font.Name =”Calibri”
#$Table.Cell(1,1).Range.shading.BackgroundPatternColor = $NTS
#$Table.Cell(1,3).Range.shading.BackgroundPatternColor = $NTS
$rows = Import-CSV $CSV -Header column1,fred,ginger,henry
$R = 1
ForEach ($row in $rows) {
$row.column1 # accessing the contents of "column1"
$Table.Cell($R,1).PreferredWidthType = 3
$Table.Cell($R,1).Width = $TC1
$Table.Cell($R,1).Range.Text = $row.column1
$Table.Cell($R,2).PreferredWidthType = 3
$Table.Cell($R,2).Width = $TC2
$Table.Cell($R,2).Range.Text = $row.fred
$Table.Cell($R,3).PreferredWidthType = 3
$Table.Cell($R,3).Width = $TC2
$Table.Cell($R,3).Range.Text = $row.ginger
$Table.Cell($R,4).PreferredWidthType = 3
$Table.Cell($R,4).Width = $TC2
$Table.Cell($R,4).Range.Text = $row.henry
$R = ($R+1)
}
$selection.EndKey(6)
$selection.TypeParagraph()
```

Thanks Don, have amended and got the table output, just needs tweaking a bit now!

Anyone that can help getting this table inside the cell of a main table? Guess that’d be nested tables, but haven’t found anything that works -yet!

&nbsp;

---

<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:34pm UTC](https://forums.powershell.org/t/using-get-content-from-csv-file-but-wanting-to-format-to-word-table/11107/9 "2024-05-16T20:34:36Z")

</div>


