I store file path in variable as below
$body = E:\Folder\body.txt
And try to access it at multiple areas in a PowerShell script like below
Clear-content -$body
Get-content $body.ToString()
Set-content $body
But all these three type of passing arguments are not working. I am getting errors below.
Cannot find path 'C:\Users\S51\-' because it does not exist
You cannot call a method on a null-valued expression
Cannot bind argument to parameter 'Path' because it is null
Only the traditional
Clear/Get/Set-content E:\Folder\body.txt
methods work.
Is there any way to assign path to a variable and use them across whole code because I need to access the same path multiple times & if I need to modify the file path in future it requires to modify everywhere. If it is a variable, I can just modify at one place.