JMar JMar - 1 year ago 131
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

is looking for the
on my desktop - not the

$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 Source

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

$a =  Get-Content $file.fullname | select -first 1 -skip 9 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download