1

My script runs from
C:\ABC\DEF\Tools\Powershell\Development>myscript.ps1

I want to use this file in myscript.ps1 from this Relative path location
'.\AOI\UDT\testfile.text'

I cannot use absolute path 'C:\ABC\DEF\AOI\UDT\testfile.text' because it will be running in different machine later.

<# Import Script for testing>  
<. $PSScriptRoot\myscript.ps1>  
<$filpath = '.\AOI\UDT\testfile.text'>
<$Routine = Get-Content -path $filpath -ErrorAction Stop>

Error I get
Cannot find path 'C:\ABC\DEF\Tools\Powershell\Development\AOI\UDT\testfile.text' because it does not exist

I am new to programming. Can anyone help me please?
Thanks

  • Can you include the content of your script (or part of it having this problem) into the question? – Kingsley Jan 25 '19 at 00:10
  • # Import Script for testing . $PSScriptRoot\myscript.ps1 $filpath = '.\AOI\UDT\testfile.text' $Routine = Get-Content -path $filpath -ErrorAction Stop – Radhik Patel Jan 25 '19 at 00:20

2 Answers2

0

Relative to the directory you are executing the script in, the path to your testfile.text would be:

$filepath = "..\..\..\AOI\UDT\testfile.text"
ethan0807
  • 1
  • 1
0
$filepath = (Resolve-Path ($PSScriptRoot + "\..\..\..\AOI\UDT\testfile.text"))

This worked best for me.