Simpleton
Simpleton

Reputation: 3

Assign full path of the file to a variable Powershell

I am creating a script for windows. Idea is to schedule it to a daily basis using Task Manager. The diffuculty is that i want to make this script portable, so whenever someone runs it on thiers computer, it should work . I need to make path not "hard coded", so the file could be found wherever it was downloaded.

Write-Host "Windows1"


$pathname= get-childitem "C:\Users\" -recurse | Where-Object {$_.name  -Like "ScheduledUpdateAndScan.ps1"} 

$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument $pathname

$trigger =  New-ScheduledTaskTrigger -Daily -At 11am

Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "Windows1" -Description "Windows1 daily scan routine and check for updates"

But $pathname won't take the full path

enter image description here

It only takes the actual name of the file Please, help me if you can

Upvotes: 0

Views: 1184

Answers (1)

Doug Maurer
Doug Maurer

Reputation: 8868

You have not passed it the full path.

$pathname= get-childitem "C:\Users\" -recurse | Where-Object {$_.name  -Like "ScheduledUpdateAndScan.ps1"} 

The variable $pathname contain a fileinfo object. It has many properties, one of those is call FullName. You should specify this in your command.

$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument $pathname.FullName

Upvotes: 1

Related Questions