With powershell 7 we lost the ability to Set-Location to a file path. Set-Location supports only a folder path.
This PS6 feature was a really good productivity feature.
It would be nice to add it again.
Can you share the use case for having the current location set to a file?
I'd consider that a bug if that were allowed, and without hunting down the original issue, that's probably how it was triaged.
PowerShell 5.1 does not allows this too.
Yes maybe it was in powershell 3 not 5, but it used to work in Powershell ISE.
A use case: you copy the path of a file into the clipboard, then open a command line and try to change the current directory to the folder containing this file. You have to manually delete the file part of the path to be able to change it.
It was a nice uncommon feature that helps make powershell unique, without any side effect.
You can use Split-Path to strip off the leaf fragment of the path:
$filepath | Split-Path | Set-Location
I have shared "cd as function" in place of the alias, and it has extra argument completers and transformers to allow things like cd ... (for ....). It would be trivial to add one removal of any file part to leave the path. I think this is better left as a user customization than changing the behavior of Set-Location. (but having done the customization, I would think that!)
Well Set-Location really used to select the folder part of the path it gets in previous versions of powershell and it won't break anything. Why won't you put that as a new good and practical feature, making gain lots of time, instead of simply closing it without any comment ? You are too conservative guys. Be a little open minded.
You can use Split-Path to strip off the leaf fragment of the path:
$filepath | Split-Path | Set-Location
Of course i can also write binary code to do the same. The idea is to gain time. Time is money. Time is electrical power. Gaining time is green energy. Think about that.
@softlion If the community votes for the enhancement, then it will certainly be realized.
Most helpful comment
Can you share the use case for having the current location set to a file?
I'd consider that a bug if that were allowed, and without hunting down the original issue, that's probably how it was triaged.