Run Powershell as administrator from command line?

Is there a way to execute Powershell 'as administrator' from command line?  When I open a command window on the Server and select 'Run As Administrator' then run my main script that then calls this script it works great.  If I try and schedule the main script in Task Scheduler this part doesn't run.  Thus was thinking a permissions issue.  Not really sure as nothing writes to my log file......

@echo off

if not exist %ARCHIVEDIR%\PBCS\Backups\%Timestamp% mkdir %ARCHIVEDIR%\PBCS\Backups\%Timestamp%

setlocal
set Source=%FILEDIR%\Backup_Data\%AppName%\%AppName%_FY*_Export_SUI.txt
set Target=%ARCHIVEDIR%\PBCS\Backups\%Timestamp%\%AppName%_Data.zip
powershell.exe -Command "Get-ChildItem '%Source%' | Compress-Archive -DestinationPath '%Target%' -Force"


exit /b

Open in new window

elwayisgodAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sam JacobsDirector of Technology Development, IPMCommented:
You can add this snippet to your script to make sure that it is being run in an elevated session:
# make sure script is running elevated
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
    Write-Warning "This script needs to be run elevated.`nPlease re-run this script as an Administrator!"
    Break
}

Open in new window

elwayisgodAuthor Commented:
Not a PS person at all, where do I put it?  Above?
elwayisgodAuthor Commented:
Like so?

@echo off

if not exist %ARCHIVEDIR%\PBCS\Backups\%Timestamp% mkdir %ARCHIVEDIR%\PBCS\Backups\%Timestamp%

setlocal
set Source=%FILEDIR%\Backup_Data\%AppName%\%AppName%_FY*_Export_SUI.txt
set Target=%ARCHIVEDIR%\PBCS\Backups\%Timestamp%\%AppName%_Data.zip


# make sure script is running elevated
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
    Write-Warning "This script needs to be run elevated.`nPlease re-run this script as an Administrator!"
    Break
}


powershell.exe -Command "Get-ChildItem '%Source%' | Compress-Archive -DestinationPath '%Target%' -Force"



exit /b

Open in new window

Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Sam JacobsDirector of Technology Development, IPMCommented:
Sorry ... I answered your question without looking at your script - which is a command script - not a PowerShell script, so my snippet won't help for that.
Philip ElderTechnical Architect - HA/Compute/StorageCommented:
In Task Scheduler make sure to tick the options: Run while user not logged on, and Run elevated. The task will prompt for credentials.

That should do it.
elwayisgodAuthor Commented:
Those are checked in Task Scheduler.  Weird.
elwayisgodAuthor Commented:
TaskScheduler.jpg
elwayisgodAuthor Commented:
Let me do some more troubleshooting.  Problem is it's not behaving same when running from command prompt so seeing the error is impossible as I pipe to log file via >, but nothing shows up...
elwayisgodAuthor Commented:
It's resolved.  Wasn't administrator privilege issue.  Sorry for confusion.  Issue was the files in the source directory for the powershell script were not there.  Had to adjust the 'Start In' directory of the Task Scheduler to get the tool to download to the source directory so the .zip could actually work.
elwayisgodAuthor Commented:
See above

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.