Skip to content

Instantly share code, notes, and snippets.

@idiotandrobot
Created February 28, 2026 09:23
Show Gist options
  • Select an option

  • Save idiotandrobot/0ad9159dd8ae938f3f5d1e7de112d9f2 to your computer and use it in GitHub Desktop.

Select an option

Save idiotandrobot/0ad9159dd8ae938f3f5d1e7de112d9f2 to your computer and use it in GitHub Desktop.
function Get-DownloadFolderPath() {
$SHGetKnownFolderPathSignature = @'
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
public extern static int SHGetKnownFolderPath(
ref Guid folderId,
uint flags,
IntPtr token,
out IntPtr lpszProfilePath);
'@
$GetKnownFoldersType = Add-Type -MemberDefinition $SHGetKnownFolderPathSignature -Name 'GetKnownFolders' -Namespace 'SHGetKnownFolderPath' -Using "System.Text" -PassThru
$folderNameptr = [intptr]::Zero
[void]$GetKnownFoldersType::SHGetKnownFolderPath([Ref]"374DE290-123F-4565-9164-39C4925E467B", 0, 0, [ref]$folderNameptr)
$folderName = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($folderNameptr)
[System.Runtime.InteropServices.Marshal]::FreeCoTaskMem($folderNameptr)
$folderName
}
Export-ModuleMember -Function Get-DownloadFolderPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment