liveBook · Manning

Namespace

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

A namespace is a naming scope that groups related types and helps keep their identifiers distinct from types in other scopes.

A namespace is a naming scope that organizes related types and keeps identifiers distinct. In .NET, a dotted name can identify a namespace hierarchy rather than a class. section 25.2

Use namespaces to make custom types recognizable and safely owned:

  • Avoid reserved or vendor-owned prefixes such as System. section 24.2.2
  • Build a hierarchy from an organization or team name, such as MyCompany.ITOps.Storage. section 24.2.2
  • Use documentation to distinguish a namespace from the classes, enumerations, and other types beneath it. section 25.2

A custom PowerShell output type

A disk-information function assigns the type name Toolmaking.DiskInfo to each output object. A team could instead choose MyCompany.ITOps.Storage.DiskInfo, creating a namespace that is less likely to overlap with another group’s types. section 24.2.2

Listing 24.2 Adding a custom type name to an object
function Get-DiskInfo {
 foreach ($domain in (Get-ADForest).domains) { 
   $hosts = Get-ADDomainController -filter * -server $domain | 
   Sort-Object -Prop hostname
   ForEach ($h in $hosts) { 
    $cs = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $h
    $props = @{'ComputerName' = $h
               'DomainController' = $h
               'Manufacturer' = $cs.manufacturer
               'Model' = $cs.model
               'TotalPhysicalMemory(GB)'=$cs.totalphysicalmemory / 1GB
               }
     $obj = New-Object -Type PSObject -Prop $props              #1
     $obj.psobject.typenames.insert(0,'Toolmaking.DiskInfo')    #2
     Write-Output $obj
   } #foreach $h
  } #foreach $domain
} #function
Export-ModuleMember -function Get-DiskInfo

Inspect the complete PowerShell listing that creates an object and inserts its custom namespace-qualified type name. section 24.2.2

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Defines namespaces, distinguishes them from classes, and demonstrates hierarchical naming without descending into namespace internals.
Examples
Uses System.Speech.Synthesis, Toolmaking.DiskInfo, and MyCompany.ITOps.Storage.DiskInfo as naming examples.
What it assumes
Assumes familiarity with PowerShell objects and .NET classes while teaching namespace-versus-type distinctions directly.
What you can do after
Apply it via hierarchical names that identify custom types without intruding on vendor-controlled namespaces.
How it connects
Connects namespaces with type names, classes, enumerations, assemblies, and PowerShell output objects.

Also explained in

Compare how 20 books explain namespace →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.