liveBook · Manning

OutputType attribute

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

The OutputType attribute declares the .NET types of objects that a PowerShell function or script is expected to output.

The OutputType attribute declares the .NET type of the value a PowerShell function is expected to return. For an archive-path function that returns a file name, use [OutputType([string])]. section 2.1.1

Use the attribute as part of a maintainable function or script structure:

  • Place [OutputType()] after [CmdletBinding()] at the beginning of a function. section 2.1.1
  • Declare a specific type when the function has a known output, such as [string] for an archive file path. section 2.1.1
  • Include OutputType attributes alongside comment-based help, CmdletBinding, and a parameter block in reusable scripts. section 2.1.7

Declaring an archive-path result

Function Set-ArchiveFilePath { [CmdletBinding()] [OutputType([string])] param( [Parameter(Mandatory = $true)] [string]$ZipPath, [Parameter(Mandatory = $true)] [string]$ZipPrefix, [datetime]$Date = (Get-Date) ) # Builds and returns the archive file path. } $ZipFile = Set-ArchiveFilePath @ZipParameters # L:\Archives\LogArchive-20210110.zip section 2.1.1 section 2.1.7

Listing 2.1 Set-ArchiveFilePath function
Function Set-ArchiveFilePath{                                   #1
    [CmdletBinding()]                                           #2
    [OutputType([string])]
    param(                                                      #3
    [Parameter(Mandatory = $true)]
    [string]$ZipPath,
 
    [Parameter(Mandatory = $true)]
    [string]$ZipPrefix,
 
    [Parameter(Mandatory = $true)]
    [datetime]$Date
    )
 
    if(-not (Test-Path -Path $ZipPath)){                        #4
        New-Item -Path $ZipPath -ItemType Directory | Out-Null
        Write-Verbose "Created folder '$ZipPath'"               #5
    }
    
    $timeString = $Date.ToString('yyyyMMdd')                    #6
    $ZipName = "$($ZipPrefix)$($timeString).zip"                #7
    $ZipFile = Join-Path $ZipPath $ZipName                      #8
 
    if(Test-Path -Path $ZipFile){                               #9
        throw "The file '$ZipFile' already exists"
    }
 
    $ZipFile                                                    #10
}

Inspect the complete Set-ArchiveFilePath implementation, including its declared string output and path-handling logic. section 2.1.1

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Explains placement and a string declaration, but does not detail type-system internals or output validation.
Examples
Uses Set-ArchiveFilePath to declare and return a string archive path for Compress-Archive.
What it assumes
Assumes basic PowerShell functions, parameters, and attributes while reinforcing reusable automation practices.
What you can do after
Apply it by annotating reusable functions and scripts with the types their automation outputs.
How it connects
Connects OutputType with CmdletBinding, advanced functions, parameter blocks, comment-based help, and reusable scripts.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.