# Parameters - adjust these values as needed $RootPath = "C:\Path\To\Site" # Local directory containing the website files $BaseUrl = "https://example.com" # Base URL of the site (no trailing slash) # Gather files that should appear in the sitemap $FilePatterns = @("*.html","*.htm","*.php","*.aspx","*.asp") $Files = Get-ChildItem -Path $RootPath -Recurse -File -Include $FilePatterns # Prepare XML writer settings $settings = New-Object System.Xml.XmlWriterSettings $settings.Indent = $true $settings.Encoding = [System.Text.Encoding]::UTF8 # Create sitemap.xml in the root directory $SitemapPath = Join-Path $RootPath "sitemap.xml" $writer = [System.Xml.XmlWriter]::Create($SitemapPath, $settings) # Write XML header and root element $writer.WriteStartDocument() $writer.WriteStartElement("urlset") $writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9") foreach ($file in $Files) { # Compute relative path from site root $relative = $file.FullName.Substring($RootPath.Length).TrimStart('\','/') # Convert filesystem separators to URL separators and escape each segment $segments = $relative -split '[\\/]' | ForEach-Object { [System.Uri]::EscapeDataString($_) } $urlPath = ($segments -join '/') # Special handling for default documents (e.g., index.html at any folder) if ($urlPath -match '^(.*?)(index\.html?|default\.aspx?)$') { $urlPath = $matches[1].TrimEnd('/') } $loc = "$BaseUrl/$urlPath".TrimEnd('/') $lastMod = $file.LastWriteTimeUtc.ToString("yyyy-MM-ddTHH:mm:ssZ") # Write entry $writer.WriteStartElement("url") $writer.WriteElementString("loc", $loc) $writer.WriteElementString("lastmod", $lastMod) $writer.WriteElementString("changefreq", "weekly") $writer.WriteElementString("priority", "0.5") $writer.WriteEndElement() # } # Close root element and document $writer.WriteEndElement() # $writer.WriteEndDocument() $writer.Flush() $writer.Close() Write-Host "Sitemap generated at:" $SitemapPath