Change Directory in PowerShell is a terminology to navigate the file system through the cmdlets. The script works with the current files and folders or the relative path of the files and folders.
So in this blog, we are talking about how you can change the Directory in PowerShell. We provide various examples; you can follow them and change the Directory.
Some Ways To Change Directory In PowerShell
- Change To Root Directory
- Change Directory to a Specified Path
- Change to a Directory in The Current Directory
- Change Directory in PowerShell with Spaces
- Change Directory in PowerShell to a Registry Path
- Change Directory in PowerShell to Desktop
- Change Directory one step from Current Directory
- Change Directory in PowerShell from C & D
- Change Directory in PowerShell Script
- Change Directory in PowerShell to Program Files
Method 1: Change To Root Directory
This method can change the Directory in PowerShell to the root directory. So follow the below steps.
- Enter this “Set-Location \” command below and press enter to change the Directory in PowerShell to the root directory.
- “C:\Users\Victo” is the PowerShell Prompt location. Put the “Set-Location \” command and press enter to change to the root of C. The Directory is now the root of C.
Method 2: Change Directory to a Specified Path
Now, we can change the Directory to a specific path because PowerShell allows us to change the Directory to a particular path.
- Enter Set-Location followed the Path parameter, and complete the path to change directories.
Set-Location -Path [full path]
- We will change to “C:\G-Drive”. Put this command and click the enter button.
Set-Location -Path “C:\G-Drive”
- You can see the PowerShell prompt will change to the new Directory.
Method 3: Change to a Directory in The Current Directory
- Enter this the Get-Childltem command to list all directories in the current directory.
- In the “C:\G-Drive” Directory, I have two folders “, Operations Tools” and “Work Tools”.
- Enter this Set-Location “Operations Tools” command to change to “Operations Tools”.
- After that, the PowerShell prompt will change to the new Directory immediately.
Method 4: Change Directory in PowerShell with Spaces
In this step, we will be changing to a directory with spaces. It is straightforward to change the Directory with spaces.
- You can use this Set-Location “D:\G-Drive-P\Work Tools” command if you change to the d:\G-Drive-P\Work Tools directory in PowerShell.
- You will receive an error if you run the same command without enclosing D:\G-Drive-P\Work Tools in double-quotes.
Set-Location D:\G-Drive-P\Work Tools
- After that, this command changes the Directory in PowerShell to one with spaces to surround the Directory in double quotations.
Method 5: Change Directory in PowerShell to a Registry Path
Now we change the Directory in PowerShell to a Registry Path.
- We are providing some registry root keys given below
- HKEY_LOCAL_MACHINE-HKLM
- HKEY_CURRENT_CONFIG-HKCC
- HKEY_CLASSES_ROOT-HKCR
- HKEY_ISERS-HKU
- HKEY_CURRENT_USER-HKCU
- Enter the abbreviation of the root key followed by “:\”- in the Set-Location to change the Directory to any of these registry keys in PowerShell.
- In this command, we want to change the Directory to HKEY_LOCAL_MACHINE…
Set-Location HKLM:\
After that, it changes the Directory in PowerShell.
Method 6: Change Directory in PowerShell to Desktop
We can use a PowerShell environment variable to access program Files. PowerShell has an environment variable for the path to the current user’s profile.
- $evn:USERPROFILE is the currently logged-on user’s profile.
- You can see your user profile by running the above command.
- When you open the path to a user’s profile in Windows 10, then u=you can see a folder called “Desktop”.
- If you change the Directory in PowerShell to the Current user’s desktop, run the below command.
$Desktop = $evn:USERPROFILE + “\desktop”
Set-Location $Desktop
- You can save the location of the desktop folder to a variable. $evn:USERPROFILE + “\desktop directly in the Set-Location command was throwing all sorts of errors.
- After that, it changes the Directory in PowerShell to desktop.
Method 7: Change Directory in one step from the Current Directory
In this step, we will talk about how to change the Directory in one step from the current Directory.
- Enter this Set-Location .. command and press enter to change Directory one Directory behind the current Directory.
- When your current PowerShell Prompt is on the “C:\Users\Victo” directory, suppose you want to change the PowerShell directory to the “C:\Users” directory.
Method 8: Change Directory in PowerShell from C & D
Now, we can change from one drive to another in PowerShell prompt. So, we can change from drive “C:” to drive D:
- Enter Set-Location D: command and press enter to change the drive.
- After that, the PowerShell prompt will change from the previous directory “C:” to the new “D:” drive.
Method 9: Change Directory in PowerShell Script
If you run a PowerShell script, there are instances when you want to access your script’s location.
There is an automatic variable called “$Script:MyInvocation.MyCommand.Path”. You can use this variable when you locate the Directory your script is running from. But you can see this variable when a PowerShell script is executing.
To apply this, you can copy and paste the following script into PowerShell script and save AccessScriptDirectory.
This script creates a report of the last 5 logs in the “System”, “Application”, and “Security” event logs.
We saved the “$Script:MyInvocation.MyCommand.Path” in the third to the last line automatic variable to a named variable called $ReportPath.
$CSVReportFile we create another variable on the second to the last line. This variable is the path to the CSV file. After that, export the event log report to the CSV file. The script is saved in "D:\PS-Tutorial" and the CSV file is created in this location. #Create a single-key hashtable (LogName) with values - "System", "Application", "Security" $LogNameKeys = @{ LogName = @("System", "Application", "Security") } #Convert the hashtable to a string $LogNameKeysAsStrings = $LogNameKeys.Values | out-string -stream #Uses ForEach to loop through $LogNameKeysAsStrings and run the Get-EventLog command. #Then, save the output of the command in the EventLogReport variable, $EventLogReport = ForEach ($LogNameKey in $LogNameKeysAsStrings) { Get-EventLog -LogName $LogNameKey -Newest 5 } $ReportPath = Split-Path -Parent $Script:MyInvocation.MyCommand.Path $CSVReportFile = "$ReportPath + "EventLogsReport.csv" $EventLogReport | Export-Csv -Path $CSVReportFile -NoTypeInformation
Save this script and then open PowerShell as administrator. Search the location you saved the script and execute the “. \AccessScriptDirectory.ps1” command.
Now, open the above script when you save the script. You can see the CSV report saved in the same folder.
Method 10: Change Directory in PowerShell to Program Files
We can try changing the PowerShell Directory to program files in the last method. We know that Windows 10 has two Program File paths: \Program Files (x86) and C. Now we change the directories in PowerShell in environment variables. Because it is the best way.
So, the environment variable for \Program Files (x86) is ${evn:ProgramFiles(x86)}.
And the environment variable for \Program Files (x86) is ${evn:ProgramFiles}.
Now, run the below command to change the program file directories in PowerShell.
Set-Location ${env:ProgramFiles(x86)}
Set-Location ${env:ProgramFiles}
After that, you can see PowerShell in the program files.
I hope you can successfully change the Directory in PowerShell.
Leave a Reply