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



















