0

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

1
  • $fullname1= get-childitem "C:\Users\" -recurse | Where-Object {$_.name -Like "ScheduledUpdateAndScan*"} Write-Host $fullname1 Commented Apr 2, 2021 at 5:24

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

I highly recommend reading and living this document Tips on writing cross platform PowerShell

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.