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.