PowerShell FileSystemObject GetFolder of "." returns home directory, not pwd

Mark Renton 20 Reputation points
2025-08-28T17:31:52.01+00:00

CD to a different directory (other than your home), then do this in PowerShell (5.1.26100.4768 or 7.5.2)

PS> "Current Working Directory = $pwd"

PS> $fso = New-Object -ComObject Scripting.FileSystemObject

PS> $dot = $fso.GetFolder(".")

PS> $dot.Path

(your ~ directory will be listed here)

PS> $dot = $null

PS> $fso = $null

There is a workaround: use $pwd instead of "." in the GetFolder call

HOWEVER, the "." is treated properly if you call GetFolder from VBScript

PS> CAT FSDOT.VBS

Dim fso, Folder

Set fso = CreateObject("Scripting.FileSystemObject")

Set Folder = fso.GetFolder ( "." )

WScript.Echo Folder.Path, Folder.Size

PS> CSCRIPT.EXE //NOLOGO FSDOT.VBS

C:\NEW\VBS 8367182

(Don't do this in your home directory -- it'll error out:

OperationStopped: 0x800A0046 (CTL_E_PERMISSIONDENIED)

because Microsoft decided to put folders in your home directory which you can't search (or get the size of).)

I only discovered this problem because I wanted to get folder sizes inside PowerShell (but the Size property is missing from folder objects in PowerShell); so I resorted to use the FileSystemObject GetFolder .Size

Windows for home | Windows 11 | Apps
{count} votes

Accepted answer
  1. Francisco Montilla 11,255 Reputation points Independent Advisor
    2025-08-28T18:22:21.1233333+00:00

    Hey Mark,

    You're running into a scope mismatch. Scripting.FileSystemObject resolves "." against the process working directory, while PowerShell's $pwd is the shell's current location. Those two can drift apart in PowerShell, so "." ends up bound to the process working dir (often your profile folder) instead of the location you Set-Location'd to. VBScript doesn't show this because WSH keeps its working directory aligned with the script context.

    Before creating the COM object, sync the process working directory to the shell location, then call GetFolder(".").

    # Move to the folder you actually want
    Set-Location C:\New\Vbs
    
    # Make the process working directory match $pwd
    [System.Environment]::CurrentDirectory = (Get-Location).Path
    
    # Now COM resolves '.' correctly
    $fso = New-Object -ComObject Scripting.FileSystemObject
    $dot = $fso.GetFolder(".")
    $dot.Path   # -> C:\New\Vbs
    $dot.Size   # works here too
    

    You can verify the desync first with [System.Environment]::CurrentDirectory. If it shows your home while $pwd doesn't, you've reproduced the cause. If you want this to just work every time, put the Environment.CurrentDirectory line in your PowerShell profile so it runs once per session, right after you set your preferred startup location.

    You found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.