How to Correctly Find the Downloads Folder in PowerShell?

Mark Moore 21 Reputation points
2024-01-19T04:44:10.4933333+00:00

There are a number of posts on the web effectively saying to concatenate %UserProfile% and "Downloads" in order to find the path to the Downloads special folder. That is just wrong because a) "Downloads" is not localized, and b) because the user is able to change the location of the target folder (which I have done). One of The Scripting Guys posts says to use [environment]::getfolderpath() all to fetch special folders.[1] But, I have not found a documented list of valid strings to pass to that function and "downloads" is definitely not allowed.[2] Until .Net decides to fix [environment]::getfolderpath(), it looks like the only way to reliably find the path to the user's special Downloads folder is to do a bunch of really gross Win SDK calls. (See the CExplorerBrowserHostDialog::_FillViewWithKnownFolders() example in the Win7Samples up on GitHub.[3]) 🤮🤮🤮 Does anyone know if there is an easier way to do this? [1]: https://devblogs.microsoft.com/scripting/the-easy-way-to-use-powershell-to-work-with-special-folders/ [2]: https://github.com/dotnet/runtime/issues/554 [3]: https://github.com/microsoft/Windows-classic-samples/blob/1d363ff4bd17d8e20415b92e2ee989d615cc0d91/Samples/Win7Samples/winui/shell/appplatform/ExplorerBrowserCustomContents/ExplorerBrowserCustomContents.cpp

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
4,770 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,081 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sachith Lakmal 126 Reputation points
    2024-01-19T07:21:58.7633333+00:00

    In PowerShell, you can use the Shell.Application COM object to get the real path of the Downloads folder, regardless of its location. Here is a simple example: $downloadsPath = (New-Object -ComObject Shell.Application).Namespace('shell:Downloads').Self.Path