1

Is there a way to use either KNOWNFOLDERID or CSIDL in PowerShell?

I need to be able to access some "known folders" via PowerShell script on a variety of systems. The problem is that those folders' names tend to differ between languages and Environmental Variables list (e.g. $env:something) does not contain folders I am interested accessing in (e.g. C:\Users).

joe
  • 8,344
  • 9
  • 54
  • 80
streamofstars
  • 705
  • 1
  • 10
  • 14

1 Answers1

3

Use the .NET method System.Environment.GetFolderPath like so:

[Environment]::GetFolderPath([Environment+SpecialFolder]::ApplicationData)

The MSDN topic on the Environment.SpecialFolder enum shows all the possible special folders you can ask for.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • 1
    Same thing, different syntax: `[environment]::getfolderpath("ApplicationData")`. [Reference link](http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/20/the-easy-way-to-use-powershell-to-work-with-special-folders.aspx). +1 – Victor Zakharov Oct 27 '13 at 20:07
  • Sure but you don't get tab completion on the string version of the enum value. :-) – Keith Hill Oct 27 '13 at 20:08
  • Yep, but you can pass these via command line. Depending on what OP wants to do, it may be useful to know about both notations. :) – Victor Zakharov Oct 27 '13 at 20:09
  • 1
    Absolutely good to know both ways. I will use the string version when I know the enum value off the top of my head and the `[]::` version to tab complete when I don't know the enum values. – Keith Hill Oct 27 '13 at 20:12
  • So I guess the short answer to my question would be "no"? Thanks for the work around with .net, though. – streamofstars Oct 28 '13 at 22:48
  • If the .NET workaround isn't good enough, you can try PInvoking out to SHGetFolderPath. Have a look at the help for Add-Type. There are some examples that show pinvoking to the Win32 API. – Keith Hill Oct 28 '13 at 23:28