null_pointer
null_pointer

Reputation: 1849

Often used directories

I have about 3 or 4 directories I go to often on my machine and would love a way to easily go straight to these directories instead of always typing them.

The best way I could think of to do this is set environment variables. However doing "cd env:" doesn't work.

Anyway have any ideas on the best way to do this?

*Edit 1* I'm hoping for a approach that I don't lose once my session is close(for example, closing PS window.).

Upvotes: 2

Views: 117

Answers (5)

Knuckle-Dragger
Knuckle-Dragger

Reputation: 7046

Some readers may prefer the machine wide profile over the per user $profile. If so, edit or create a file in this location.

%windir%\system32\WindowsPowerShell\v1.0\profile.ps1

IIRC, it runs before the user profile is loaded, whether that is advantage / disadvantage you decide.

Upvotes: 1

Chriseyre2000
Chriseyre2000

Reputation: 2053

Get your profile using notepad $profile from within powershell.

Put one of the above functions in there and restart powershell.

Upvotes: 2

mjolinor
mjolinor

Reputation: 68243

You can create a PS drive for each one in your profile:

New-PSDrive Dir1 -PSProvider FileSystem -Root 'c:\windows\system32'
New-PSDrive Dir2 -PSProvider FileSystem -Root 'c:\program files\Common Files'
New-PSDrive Dir3 -PSProvider FileSystem -Root 'C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys'

Then just CD or SL to the drive name:

cd dir1:
sl dir2:

Upvotes: 5

alroc
alroc

Reputation: 28154

Create a small function in your profile for each.

function gohome {
    set-location c:\users\username
}

Upvotes: 4

user189198
user189198

Reputation:

You can create a HashTable in your PowerShell profile script that points to the various folders. Then, simply reference them using the short-hand:

$FL = @{
    Dir1 = 'c:\windows\system32'
    Dir2 = 'c:\program files\Common Files'
    Dir3 = 'C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys'
}

cd $FL.Dir1;
cd $FL.Dir2;
cd $FL.Dir3;

Alternatively, you could develop small functions, and place them into your PowerShell profile script.

function sys32 {
    [CmdletBinding()]
    param ()
    Set-Location -Path 'c:\windows\system32';
}

function mkeys {
    [CmdletBinding()]
    param ()
    Set-Location -Path 'C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys';
}

function cf {
    [CmdletBinding()]
    param ()
    Set-Location -Path 'C:\Program Files\Common Files';
}

# Call the functions
sys32;
mkeys;
cf;

Upvotes: 4

Related Questions