# PSWriteWord – Version 0.5.1

During last couple of weeks I’ve spent most of my time developing PSWinDocumentation project. While building that I’ve came across different…

blog

21 Aug 2018 pl

![PSWriteWord – Version 0.5.1](/wp-content/uploads/2018/08/2018-08-21-17_41_23-Window.png)

[Zobacz obrazy (11)](#pf-content-image-1-0)

During last couple of weeks I've spent most of my time developing [**PSWinDocumentation**](https://evotec.xyz/hub/scripts/pswindocumentation-powershell-module/)project. While building that I've came across different problems that needed solutions and consistent output. In this release **0.5.X **I've addressed some of them and also built what I call a **building blocks**. While not necessary a functionality of Word it allows me to easily built [**PSWinDocumentation**](https://evotec.xyz/hub/scripts/pswindocumentation-powershell-module/)with just one command.

> Please notice this article **contains parts of information **(still useful) and **may not reflect all functionalities of this module**. For download, source code and so on you should refer to the dedicated **[PSWriteWord](https://evotec.xyz/hub/scripts/pswriteword-powershell-module/)** module page. After reading this one… of course! It contains useful *information*, *examples *and *know-how*.

### 💡 Building blocks

While I'm not sure it will please everyone or will it be functional outside of my personal need it's there for you to use. Keep in mind that this will be further enhanced, changed so may need some revisits later on. Right now there are 5 functions but you should really worry about one… **New-WordBlock**.

- New-WordBlock
- New-WordBlockList
- New-WordBlockPageBreak
- New-WordBlockParagraph
- New-WordBlockTable

### 💡 What is block

Block is combination of multiple Word elements that can be added or not. Seems weird I know… but it's essentially a combination of multiple elements that play well together. For example a **TOC** **Element**, a **TEXT** and **TABLE** after it is one building block. But that building block doesn't have to have a **TOC Element**. It can be just **TEXT** and **TABLE**. It can also be a **TOC, TEXT,TABLE, EMPTY PARAGRAPH, PAGE BREAK **and **CHART** as one building block. In other words if you pass data to function you will get it displayed, if you don't… it will not appear. This allows you to create **Word Document (docx) **with documentation of your environment in no time. You want **Text** and a **Table** great – pass two variables and it's all good. But wait.. there's more. If you use **TextNoData** element it will display alternative text to the one you defined in **Text** field if **TableData**, **ListData** turns up empty. This allows you to build general documentation without actually knowing what will the output be.

- 🔧 Can be a TOC element (or lack of it)
- 🔧 Can be a Text (or lack of it)
- 🔧 Can be a chart (or lack of it)
- 🔧 Can be a page break (or lack of it, or multiple of those) – just add 5 to have 5 page breaks. Before or After..

It generally looks like this in code. One command.. that can generate multiple, different type of blocks.

```powershell
        $WordDocument | New-WordBlock `
            -TocGlobalDefinition $Section.TocGlobalDefinition`
            -TocGlobalTitle $Section.TocGlobalTitle `
            -TocGlobalSwitches $Section.TocGlobalSwitches `
            -TocGlobalRightTabPos $Section.TocGlobalRightTabPos `
            -TocEnable $Section.TocEnable `
            -TocText $TocText `
            -TocListLevel $Section.TocListLevel `
            -TocListItemType $Section.TocListItemType `
            -TocHeadingType $Section.TocHeadingType `
            -TableData $TableData `
            -TableDesign $Section.TableDesign `
            -TableTitleMerge $Section.TableTitleMerge `
            -TableTitleText $TableTitleText `
            -TableMaximumColumns $Section.TableMaximumColumns `
            -Text $Text `
            -TextNoData $TextNoData `
            -EmptyParagraphsBefore $Section.EmptyParagraphsBefore `
            -EmptyParagraphsAfter $Section.EmptyParagraphsAfter `
            -PageBreaksBefore $Section.PageBreaksBefore `
            -PageBreaksAfter $Section.PageBreaksAfter `
            -TextAlignment $Section.TextAlignment `
            -ListData $ListData `
            -ListType $Section.ListType `
            -ListTextEmpty $Section.ListTextEmpty `
            -ChartEnable $Section.ChartEnable `
            -ChartTitle $ChartTitle `
            -ChartKeys $ChartKeys `
            -ChartValues $ChartValues `
            -ListBuilderContent $ListBuilderContent `
            -ListBuilderType $Section.ListBuilderType `
            -ListBuilderLevel $Section.ListBuilderLevel
```

### 💡 Block A

[!\[Przykladowy uklad bloku A w PSWriteWord\](/wp-content/uploads/2018/08/img_5b7c2744df061.png)](/wp-content/uploads/2018/08/img_5b7c2744df061.png)

### 💡 Block B

[!\[Przykladowy uklad bloku B w PSWriteWord\](/wp-content/uploads/2018/08/img_5b7c277db0ff7.png)](/wp-content/uploads/2018/08/img_5b7c277db0ff7.png)

### 💡 Block C

[!\[Przykladowy uklad bloku C w PSWriteWord\](/wp-content/uploads/2018/08/img_5b7c2797cc9d1.png)](/wp-content/uploads/2018/08/img_5b7c2797cc9d1.png)

### 💡 Block D

[!\[Przykladowy uklad bloku D w PSWriteWord\](/wp-content/uploads/2018/08/img_5b7c2afcb1b68.png)](/wp-content/uploads/2018/08/img_5b7c2afcb1b68.png)

### 💡 Block E

[!\[Przykladowy uklad bloku E w PSWriteWord\](/wp-content/uploads/2018/08/img_5b7c2b0aeddec.png)](/wp-content/uploads/2018/08/img_5b7c2b0aeddec.png)

### 💡 Block F

[!\[Przykladowy uklad bloku F w PSWriteWord\](/wp-content/uploads/2018/08/img_5b7c2b32d2fb6.png)](/wp-content/uploads/2018/08/img_5b7c2b32d2fb6.png)

### 💡 Block G

[!\[Przykladowy uklad bloku G w PSWriteWord\](/wp-content/uploads/2018/08/img_5b7c2b4d4de8c.png)](/wp-content/uploads/2018/08/img_5b7c2b4d4de8c.png)

And those are just examples. Of course it takes a moment to get used to it but when it does…

### 💡 Format-TransposeTable / Add-WordTable

Another addition is **Format-TransposeTable** function. Transpose (rotate) data is nicely described by Microsoft in their article [here](https://support.office.com/en-us/article/transpose-rotate-data-from-rows-to-columns-or-vice-versa-3419f2e3-beab-4318-aae5-d0f862209744).

[!\[Przyklad zamiany wierszy i kolumn w tabeli\](/wp-content/uploads/2018/08/img_5b7c34637eb24.png)](/wp-content/uploads/2018/08/img_5b7c34637eb24.png)

Transpose (rotate) data from rows to columns or vice versa Now what the function does in **PSWriteWord **it allows you to easily convert any **HashTable**/**OrderedDictionary** to **PSCustomObject** and vice versa. How this is useful?

[!\[Przyklad dzialania Format-TransposeTable na danych tabelarycznych\](/wp-content/uploads/2018/08/img_5b7c367c8b463.png)](/wp-content/uploads/2018/08/img_5b7c367c8b463.png)

```powershell
$myitems0 = @(
    [pscustomobject]@{name = "Joe"; age = 32; info = "Cat lover"},
    [pscustomobject]@{name = "Sue"; age = 29; info = "Dog lover"},
    [pscustomobject]@{name = "Jason another one"; age = 42; info = "Food lover"
    }
)
$myitems0 | Format-Table -AutoSize

$Transposed = Format-TransposeTable -Object $myitems0
$Transposed | Format-Table -AutoSize

$TransposedBack = Format-TransposeTable -Object $Transposed
$TransposedBack | Format-Table -AutoSize
```

And final output… (I know, I know it changed order… )

[!\[Koncowy wynik PSWriteWord po transpozycji danych tabeli\](/wp-content/uploads/2018/08/img_5b7c36a82a2cb.png)](/wp-content/uploads/2018/08/img_5b7c36a82a2cb.png)

This allows you to easily create **HashTables** or **OrderedDictionaries** and still display them differently (which is useful for Word/Excel tables). This command is part of **Add-WordTable** with switch **-Transpose**

```powershell
Import-Module PSWriteWord -Force
Import-Module ActiveDirectory

$FilePath = "$Env:USERPROFILE\Desktop\PSWriteWord-Example-Tables1.docx"

# Get basic AD information
$ADSnapshot = @{}
$ADSnapshot.RootDSE = $(
    $Info = Get-ADRootDSE
    $Info | Select-Object `
    @{label = 'Configuration Naming Context'; expression = { $_.configurationNamingContext }},
    defaultNamingContext, dnsHostName, domainControllerFunctionality, domainFunctionality,
    forestFunctionality, supportedLDAPPolicies, subschemaSubentry, supportedLDAPVersion, supportedSASLMechanisms
)
$ADSnapshot.ForestInformation = $(
    Get-ADForest | Select-Object DomainNamingMaster, Domains, ForestMode, Sites
)
$ADSnapshot.DomainInformation = $(Get-ADDomain)
# Get basic Ad information end

Clear-Host

$WordDocument = New-WordDocument $FilePath

Add-WordText -WordDocument $WordDocument -Text "Active Directory Root DSE with Transpose" `
    -FontSize 15 -CapsStyle smallCaps -Alignment both -Supress $True
Add-WordParagraph -WordDocument $WordDocument -Supress $True
Add-WordTable -WordDocument $WordDocument -DataTable $ADSnapshot.RootDSE `
    -Design LightShading -Bold $true -Color Blue -Supress $True -Transpose
Add-WordSection -WordDocument $WordDocument -PageBreak -Supress $True


Add-WordText -WordDocument $WordDocument -Text "Active Directory Root DSE without Transpose" `
    -FontSize 15 -CapsStyle smallCaps -Alignment both -Supress $True
Add-WordParagraph -WordDocument $WordDocument -Supress $True
Add-WordTable -WordDocument $WordDocument -DataTable $ADSnapshot.RootDSE `
    -Design LightShading -Bold $true -Color Blue -Supress $True
Add-WordSection -WordDocument $WordDocument -PageBreak -Supress $True


Add-WordText -WordDocument $WordDocument -Text "Active Directory ", 'Domain', ' Forest Information' `
    -FontSize 12, 12, 12 -StrikeThrough none, strike, none -Alignment center -Supress $True
Add-WordParagraph -WordDocument $WordDocument -Supress $True
Add-WordTable -WordDocument $WordDocument -DataTable $ADSnapshot.ForestInformation `
    -Design LightShading -Italic $true, $false -Bold $true, $false -ContinueFormatting -Supress $True -Verbose
Add-WordSection -WordDocument $WordDocument -PageBreak -Supress $True
Add-WordText -WordDocument $WordDocument -Text "Active Directory Domain Information" `
    -FontSize 15 -Color Green -Supress $True
Add-WordParagraph -WordDocument $WordDocument -Supress $True
Add-WordTable -WordDocument $WordDocument -DataTable $ADSnapshot.DomainInformation `
    -Design LightShading -Supress $True -Transpose
Add-WordPageBreak -WordDocument $WordDocument -Supress $True
Add-WordTable -WordDocument $WordDocument -DataTable $ADSnapshot.DomainInformation `
    -Design LightShading -Supress $True -Transpose `
    -ExcludeProperty 'ComputersContainer', 'AllowedDNSSuffixes', 'Item', 'LinkedGroupPolicyObjects' #-Verbose

Save-WordDocument $WordDocument -Language 'en-US' -Supress $True

### Start Word with file
Invoke-Item $FilePath
```

It's clearly visible on the next screenshot (left **HashTable**), right **Transpose** (and **Format-TransposeTable** behind scenes) in action…

[!\[Porownanie tabel Word bez transpozycji i po transpozycji\](/wp-content/uploads/2018/08/img_5b7c387084a7a.png)](/wp-content/uploads/2018/08/img_5b7c387084a7a.png)

As you can see thanks to this option I was able to put far more data into Word Table than I would without it.

### 💡 Other features / changes

There were far more functionality changes… but it's been a while since I added them… so well I forgot 🙂 Enjoy this new release… and report bugs as always as there are surely some…

> Please notice this article **contains parts of information **(still useful) and **may not reflect all functionalities of this module**. For download, source code and so on you should refer to the dedicated **[PSWriteWord](https://evotec.xyz/hub/scripts/pswriteword-powershell-module/)** module page. After reading this one… of course! It contains useful *information*, *examples *and *know-how*.

## Powiązane projekty

Projekty powiązane z tagami, kategoriami i treścią tego wpisu.

[Wszystkie projekty](/projekty/)

Trwa dopasowywanie projektów...

[Powrót do bloga](/blog/)

## Powiązane wpisy

- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)
- [Inspect and convert mixed Office archives with PSWriteOffice](/blog/inspect-and-convert-mixed-office-archives-pswriteoffice/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)

<!-- PowerForge markdown artifact sha256:A896D64A53F529679696F08538CA115EE4C0114CC747A994D44417FEB88BDCD4 -->
