Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a location to a Variable in PowerShell

I'm very new to PowerShell, and I'm sure this is a simple question but I'm a little stumped. I'm trying to open a Folder, sort by LastWriteTime, and open the Folder at the top of the list. I want to store that in a variable so when I call the variable I can set my location to that variable. The problem I'm having is that when I call my variable, nothing happens:

$latest = Get-Childitem C:\Main | Sort LastWriteTime -Descending | Select -First 1 | Invoke-Item

How come I get an error when I try to 'Set-Location $latest'?

like image 666
MaineMan Avatar asked Feb 21 '23 06:02

MaineMan


1 Answers

This worked for me:

$last = gci ./ | sort LastWriteTime | select -last 1
Set-Location ($last.FullName)
like image 85
Petro Franko Avatar answered Feb 27 '23 06:02

Petro Franko