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. Defaults to current location.")] [ValidateScript({ Test-Path $_ -PathType Container })] [string]$RootPath = (Get-Location).Path, [Parameter(Mandatory=$false, HelpMessage="Output sitemap file path.")] [string]$OutputFile = "$RootPath\sitemap.xml", [Parameter(Mandatory=$false, HelpMessage="File extensions to include (comma-separated).")] [string[]]$IncludeExtensions = @(".html",".htm",".aspx",".php",".asp",".jsp",".js",".css"), [Parameter(Mandatory=$false, HelpMessage="Change frequency (always, hourly, daily, weekly, monthly, yearly, never).")] [ValidateSet("always","hourly","daily","weekly","monthly","yearly","never")] [string]$ChangeFreq = "weekly", [Parameter(Mandatory=$false, HelpMessage="Priority (0.0 to 1.0).")] [ValidateRange(0.0,1.0)] [double]$Priority = 0.5 ) # Ensure BaseUrl ends with a slash if (-not $BaseUrl.EndsWith('/')) { $BaseUrl = "$BaseUrl/" } # Resolve root path $RootPath = (Resolve-Path -Path $RootPath).ProviderPath # Build a list of files $filter = $IncludeExtensions | ForEach-Object { "*$_" } $files = Get-ChildItem -Path $RootPath -Recurse -File -Include $filter -ErrorAction SilentlyContinue # Prepare XML writer settings $settings = New-Object System.Xml.XmlWriterSettings $settings.Indent = $true $settings.Encoding = [System.Text.Encoding]::UTF8 # Create the XML file $writer = [System.Xml.XmlWriter]::Create($OutputFile, $settings) $writer.WriteStartDocument() $writer.WriteStartElement("urlset") $writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9") foreach ($file in $files) { # Relative path from root $relativePath = $file.FullName.Substring($RootPath.Length).TrimStart('\','/') # Convert backslashes to forward slashes for URL $urlPath = $relativePath -replace '\\','/' # Escape URL characters $escapedPath = [System.Web.HttpUtility]::UrlPathEncode($urlPath) $loc = $BaseUrl + $escapedPath $lastMod = $file.LastWriteTimeUtc.ToString("yyyy-MM-ddTHH:mm:ssZ") $writer.WriteStartElement("url") $writer.WriteElementString("loc", $loc) $writer.WriteElementString("lastmod", $lastMod) $writer.WriteElementString("changefreq", $ChangeFreq) $writer.WriteElementString("priority", "{0:N1}" -f $Priority) $writer.WriteEndElement() # url } $writer.WriteEndElement() # urlset $writer.WriteEndDocument() $writer.Flush() $writer.Close() Write-Host "Sitemap generated at: $OutputFile" -ForegroundColor Green