Powershell – current folder and file

While working with PowerShell some times if is required to access resources like XML and csv files from local folder where the PS1 file is placed.  PowerShell 3.0 has made it easy for us.  Use the script below to get current folder, file and execution path

$thisfolderIncludingFileName = Split-Path -parent $MyInvocation.MyCommand.Definition
$currentScriptName = $MyInvocation.MyCommand.Name

$currentExecutionPath = $thisfolderIncludingFileName.Replace($currentScriptName, "");

Write-Host "Current Folder with FileName : $thisfolder"
Write-Host "Current File Name : $currentScriptName"
Write-Host "Current Exeuction Path $currentExecutionPath"

image

Note: I have find out that you must save your script file before you can test these calls and you can not get this path when you press F8 to execute any script line by line.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s