param( [Parameter(Mandatory=$true, HelpMessage="Base URL of the website, e.g., https://example.com")] [ValidatePattern('^https?://')] [string]$BaseUrl, [Parameter(Mandatory=$false, HelpMessage="Root folder to scan for web files")] [string]$Path = (Get-Location).Path, [Parameter(Mandatory=$false, HelpMessage="Output sitemap file name (full path or relative to -Path)")] [string]$Output = "sitemap.xml", [Parameter(Mandatory=$false, HelpMessage="File extensions to include (comma‑separated, without leading dot)")] [string[]]$IncludeExtensions = @('html','htm','aspx','php','js','css','xml'), [Parameter(Mandatory=$false, HelpMessage="Default value (always, hourly, daily, weekly, monthly, yearly, never)")] [ValidateSet('always','hourly','daily','weekly','monthly','yearly','never')] [string]$ChangeFreq = 'weekly', [Parameter(Mandatory=$false, HelpMessage="Default value (0.0 – 1.0)")] [ValidateRange(0.0,1.0)] [double]$Priority = 0.5 ) function ConvertTo-UrlPath { param( [string]$FullPath, [string]$RootPath, [string]$BaseUrl ) $relative = Resolve-Path -Path $FullPath -Relative -RelativeBasePath $RootPath $relative = $relative -replace '\\','/' $url = $BaseUrl.TrimEnd('/') + '/' + $relative.TrimStart('/') return $url } # Ensure the root path ends without a trailing slash for proper relative calculations $rootPath = (Get-Item -LiteralPath $Path).FullName.TrimEnd('\') # Build a list of pattern strings like *.html, *.htm … $patterns = $IncludeExtensions | ForEach-Object { "*.$_" } # Gather files $files = Get-ChildItem -Path $rootPath -Recurse -File -Include $patterns -ErrorAction SilentlyContinue if (-not $files) { Write-Warning "No files matching the specified extensions were found under '$rootPath'." exit 1 } # Resolve output path if ([System.IO.Path]::IsPathRooted($Output)) { $outputPath = $Output } else { $outputPath = Join-Path -Path $rootPath -ChildPath $Output } # Create XML writer settings $settings = New-Object System.Xml.XmlWriterSettings $settings.Indent = $true $settings.IndentChars = "`t" $settings.NewLineOnAttributes = $false $settings.Encoding = [System.Text.Encoding]::UTF8 $writer = [System.Xml.XmlWriter]::Create($outputPath, $settings) try { $writer.WriteStartDocument() $writer.WriteStartElement('urlset', 'http://www.sitemaps.org/schemas/sitemap/0.9') foreach ($file in $files) { $loc = ConvertTo-UrlPath -FullPath $file.FullName -RootPath $rootPath -BaseUrl $BaseUrl $lastMod = $file.LastWriteTimeUtc.ToString('yyyy-MM-ddTHH:mm:ssZ') $writer.WriteStartElement('url') $writer.WriteElementString('loc', $loc) $writer.WriteElementString('lastmod', $lastMod) if ($ChangeFreq) { $writer.WriteElementString('changefreq', $ChangeFreq) } if ($Priority -ge 0 -and $Priority -le 1) { $writer.WriteElementString('priority', $Priority.ToString('0.0')) } $writer.WriteEndElement() # url } $writer.WriteEndElement() # urlset $writer.WriteEndDocument() } finally { $writer.Close() $writer.Dispose() } Write-Host "Sitemap generated at: $outputPath" -ForegroundColor Green