1

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.

2
  • Not sure, but I think you want to have a look at Scopes. Possiby, you want to declare the path variable as $global:ThePathVariable Commented May 9, 2020 at 8:40
  • For a start, it's not seeing your filepath - you should use Clear-Content -Path $body and yes, if it;s using a differnt path then you probably need to define $body with a wider scope e.g. $Script:body = E:\Folder\body.txt Commented May 9, 2020 at 8:50

2 Answers 2

1

tl;dr

  • Your symptoms are all explained by the value of $body effectively being $null.

  • The problem is that E:\Folder\body.txt isn't quoted; if you quote it, your symptoms go away:

$body = 'E:\Folder\body.txt'

The bottom section of this answer explains string literals in PowerShell, and this answer explains PowerShell's two fundamental parsing modes, argument (command) mode and expression mode.


Explanation:

I store file path in variable as below

$body = E:\Folder\body.txt

Because what you intend to be string E:\Folder\body.txt isn't quoted, E:\Folder\body.txt is interpreted as a command, which means:

  • E:\Folder\body.txt is opened as a document, which means that it is opened asynchronously in Notepad.exe by default.

  • Since this operation has no output (return value), variable $body is created with value $null (strictly speaking, the [System.Management.Automation.Internal.AutomationNull]::Value value, which in most contexts behaves like $null).

All your symptoms are the result of the value of $body effectively being $null.

Sign up to request clarification or add additional context in comments.

Comments

0

The code below illustrates several ways of using a variable to operate on files.

param(
    [string] $body = "$PSScriptRoot\body.txt"    
)

if ((Test-Path -Path $body) -eq $false) {
    New-Item -Path $body -ItemType File
}

function GetContent() {
    Get-Content -Path $body -Verbose
}
GetContent

function GetContentOfFile([string] $filePath) {
    Get-Content -Path $body -Verbose
}
GetContentOfFile -filePath $body

Invoke-Command -ScriptBlock { Clear-Content -Path $body -Verbose }

Invoke-Command -ScriptBlock { param($filepath) Clear-Content -Path $filepath -Verbose } -ArgumentList $body

Set-content -Path $body -Value 'Some content.' -Verbose

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.