tl;dr
- The naive[1] answer, which typically but not always works:
"$HOME\Downloads"
(New-Object -ComObject Shell.Application).NameSpace('shell:Downloads').Self.Path
"$HOME\Downloads"
assumes two things, which aren't necessarily true:
That $HOME
, which is equivalent to environment variable USERPROFILE
($env:USERPROFILE
), is the root directory for the user's documents isn't always true, namely not with roaming profiles - only "${env:HOMEDRIVE}${env:HOMEPATH}"
reliably reflects the documents folder.
More importantly, the downloads folder may have been explicitly configured to be in an arbitrary location, unrelated to the documents location
The only robust way to determine the downloads folder's location is to ask the system for it:
PowerShell, as of PowerShell Core 7.0.0-preview.3, has no PowerShell-native way of asking the system for known folder locations.
While PowerShell has virtually unlimited access to the .NET framework and can therefore use the System.Environment
type's .GetFolderPath()
method to ask for special known folders, the designated folder for downloads is - surprisingly - NOT among them.
Only the WinAPI's Known Folders API allows retrieval of the designated downloads folders in a robust fashion, without relying on fixed relationships with other known folders:
In PowerShell, you can access it via the Shell.Application
COM server:
(New-Object -ComObject Shell.Application).NameSpace('shell:Downloads').Self.Path
For a list of all supported (shell:
-prefixed) folder identifiers, see this article.
[1] By naive I mean: a solution that one is understandably tempted to use, but which doesn't work in all situations.
Downloads
? (I assume you're talking about browser downloads since I can't think of another application that downloads to that directory by default.) What if the user's browser doesn't resolve naming conflicts using that same(#)
scheme? What if the user simply removes the anti-piracy code from your script? How does preventing multiple copies of the same file on the same computer prevent piracy, anyways? – Arnettearney