File System
Current location
Command Get-Location
returns the current folder.
Get-Location
pwd
(means print working directory, originally Unix Shell command) in PowerShell get current path of current working directory to the standard output. pwd is an alias of Get-Location cmdlet in PowerShell. $pwd automatic variable is type of System.Management.Automation.PathInfo , it has access to PathInfo members.
pwd
ET wants home - Get user's home folder
There is preset variable $HOME
that returns a logged user's home folder.
PS "ET wants home ;-)"
$HOME
Set location
Set-Location
sets working directory.
Set-Location C:\temp
Get-Location
Run a program in the set location
You can also call external application in PowerShell scripts. Please remember, that the applications must be in PATH
or you have to use explicit path. In the following example we will run git pull
command in the specific folder that is same time local git repository.
#Script to run git pull in the folder that is local repo
$current = Get-Location
$folder = "R:\python"
Set-Location $folder
git pull
Set-Location $current
Write-Host($folder + " done")
Read-Host("Ready to continue [Yes]/No ")
Get the script name and path
There is an automatic variable called $Script:MyInvocation.MyCommand.Path
. It can be used this variable to locate the directory your script is running from. Note that this variable is only available when a PowerShell script is executing.
#Use an automatic variable called “$Script:MyInvocation.MyCommand.Path” to get the script file.
$file = $Script:MyInvocation.MyCommand.Path
Write-Host("This is a script file:", $file)
#get the folder
$path = Split-Path -Parent $file
Write-Host("The script file is in folder:", $path)
Join Path
The Join-Path
cmdlet combines a path and child-path into a single path. The provider supplies the path delimiters. This secures that the right limiters will be used. Nice!
PS R:\> Join-Path -Path $HOME -ChildPath "temp"
C:\Users\salesa\temp
Test if path exist
You can use command Test-Path
is there a folder (path). The cmdlet returns True
if the path is, and False
if not.
In the following example we will test two paths, the first one exist and the second one not.
#check if the path is valid or not
#test first the user's myhome folder, should be True
$myhome = $env:USERPROFILE
Test-Path $myhome
#secondly test the folder .ssh, should be True also
$ssh = $myhome + "\.ssh"
Test-Path $ssh
#thirdly test the folder notexist, should be False
$nope = $myhome + "\notexist"
Test-Path $nope
Get files
Command Get-ChildItem
returns items in given path, so it should return files and folders.
ls
is alias for Get-ChildItem
-command (maybe you can see some familiarity with bash).
Example to count and show files&folders from the folder given by user
#count files
$folder = Read-Host("Give a folder to look")
$files = Get-ChildItem -Path $folder
$i = $files.Count
Write-Host("In the folder", $folder, "is", $i, "files&folders")
#and show files
foreach($file in $files)
{$file.Name}
Get folders only
Let's try get those items which could be folders in the user's home folder. Let's do that with the following script. Please note that we will use a property PsIsContainer
.
Get-ChildItem $HOME | Where { $_.PsIsContainer -eq $true} | Format-Table Name
Create New Items
Create a New Folder
New-Item
-cmdlet is used to create a folder. Cmdlet is used by passing the path using parameter -Path
as path of the new folder, and parameter -ItemType
as Directory.
In this example, we will create a new folder called C:\users\username\temp.
$path = $HOME + '\temp\'
New-Item -Path $path -ItemType Directory
Create a New File
New-Item
-cmdlet is usually used to create a file. Cmdlet is used by passing the path using parameter -Path
as path of the file, and parameter -ItemType
as File.
In this example, we will create a file in C:\users\username\temp Folder with name "DeleteMe.txt"
$file = 'DeleteMe.txt'
$path = $HOME + '\temp\' + $file
New-Item -Path $path -ItemType File
Copy Items
Copy-Item
-cmdlet is used to copy a file. Use it by passing the path of the file to be copied and destination path where the file is to be copied.
In this example, we will copy the file test.txt at the current folder to the folder Temp in the users home directory.
$file = '.\test.txt'
$path = $HOME + '\temp\'
Copy-Item -Path $file -Destination $path
In the following example, all text files starting with letter s
will be copied to the BackUp-folder in the users home directory.
Copy-Item -Path ($HOME + "\s*.txt") -Destination C:\Users\salesa\BackUp
Move Items
Move-Item
-cmdlet is used to move files and folders. Use it by passing the path of the file to be moved and destination path where the item will be copied.
In this example, we will move the file secrets1.txt from the current folder to the folder BackUp.
$file = 'secrets.txt'
$from = $HOME + '\' + $file
$to = $HOME + '\backup\' + $file
Move-Item -Path $from -Destination $to
Rename Item(s)
Rename-Item
changes the name of a specified item. You can rename a file or files, and also a folder. This cmdlet does not affect the content of the item being renamed.
Do not use Rename-Item to move an item, such as by specifying a path together with the new name. To move and rename an item, use the Move-Item, see above.
$current, $new = 'secrets.txt', 'topsecrets.txt'
Rename-Item -Path $current -NewName $new
Delete Item(s)
Remove-Item
-cmdlet is used to delete a file by passing the path of the file to be deleted. It is also used to delete a directory by passing the path of the directory to be deleted.
$file = 'DeleteMe.txt'
$path = $HOME + '\temp\' + $file
Remove-Item $path
It can be used also to delete a folder with all files in the folder by passing the parameter Recurse
.
Beware it will do deleting without any warnings, so be careful to what path you will give!!!
$path = $HOME + '\temp\'
ls $path
Remove-Item $path -Recurse
#really nothing to show