Skip to content

Instantly share code, notes, and snippets.

@richardhicks
Created August 25, 2026 23:26
Show Gist options
  • Select an option

  • Save richardhicks/d7f56b6eec737e9ba48cbc967a5ceb3e to your computer and use it in GitHub Desktop.

Select an option

Save richardhicks/d7f56b6eec737e9ba48cbc967a5ceb3e to your computer and use it in GitHub Desktop.
Export Nmap XML output to CSV
[Cmdletbinding()]
Param (
[Parameter(Mandatory, HelpMessage = "Enter the path to the Nmap XML file.")]
[string]$FilePath
)
If (-Not (Test-Path -Path $FilePath -PathType Leaf)) {
Write-Error "Input path '$FilePath' does not exist or is not a file."
Return
}
Try {
[xml]$Nmap = Get-Content $FilePath -ErrorAction Stop
}
Catch {
Write-Error "Input file '$FilePath' is not a valid XML file. $($_.Exception.Message)"
Return
}
$OutputPath = [System.IO.Path]::ChangeExtension((Resolve-Path $FilePath).Path, '.csv')
$Nmap.Nmaprun.Host | ForEach-Object {
$Ip = $_.address | Where-Object { $_.addrtype -eq 'ipv4' } | Select-Object -ExpandProperty addr
$Hostname = ($_.hostnames.hostname | Select-Object -First 1).name
$Port = $_.ports.port | Where-Object { $_.portid -eq '443' -and $_.state.state -eq 'open'
}
If ($Port) {
[PSCustomObject]@{
IP = $Ip
Hostname = $Hostname
Port = $Port.portid
Protocol = $Port.protocol
State = $Port.state.state
Service = $Port.service.name
}
}
} | Export-Csv -Path $OutputPath -NoTypeInformation
Write-Output "Export complete. Output file saved to $OutputPath."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment