JMar JMar - 3 months ago 25
PowerShell Question

Get Content from file path

I have a script that will email a notification when a file exists. It takes a line of text from the file and emails it to the recipient.

It works fine when the script is ran in the same folder in which the files are located in. However, if I put the script on my desktop and try to run it; the

Get-Content
is looking for the
$file
on my desktop - not the
R:\files\export
location.

$files = Get-ChildItem -path "R:\Files\export" |
where {($_.extension -eq '.old' -and $_ -like '*AM*')}

foreach ($file in $files) {
$a = Get-Content $file | select -First 1 -Skip 9
$a = $a.substring(3,16)

$emailMessage.Body = @"
The following Amazon PO has been imported to LM: </br></br>
"@ + $a


In the code above, how do I correctly specify the file path?

$a = Get-Content $file | select -first 1 -skip 9

Answer

You can use the property fullname to get the full path.

$a =  Get-Content $file.fullname | select -first 1 -skip 9