powershellvisual-studio-codepowershell-v5.1

Call file in powershell script using relative and absolute path


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


Solution

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

    This worked best for me.