none
Run the script on newly created instance window RRS feed

  • Question

  • Hi,

    I want to run the below script with admin rights,

    $Threshold = -90
    $UserProfileFolders = Get-ChildItem "$($env:SystemDrive)\Users" | ? { $_.LastAccessTime -lt ((Get-Date).AddDays($Threshold)) } | Select Name,FullName,LastAccessTime
    $WmiUserProfiles = Get-WmiObject Win32_UserProfile 
    $WmiUserProfiles | % {
        if (($UserProfileFolders | Select -Expand FullName) -contains $_.LocalPath) {
            $_.Delete()
        }
    }

    if run the below command on powershell, it would open the new window with admin rights,

    Start-Process powershell -Verb runAs

    But i want to run the above script on that newly opened window through scripting itself without manually copying and pasting it on the new window

    Could you please provide some insights how to do it?


    • Moved by Bill_Stewart Wednesday, September 4, 2019 9:11 PM This is not "scripts on demand"
    Thursday, April 11, 2019 11:38 AM

All replies

  • Start-Process powershell -Verb runAs-ArgumentList '-file myscript.ps1'

    Please fix your post and post the code correctly using the code posting tool. The code, as posted, is unreadable in most browsers.


    \_(ツ)_/


    • Edited by jrv Thursday, April 11, 2019 11:48 AM
    Thursday, April 11, 2019 11:45 AM
  • Thanks, can i append the script directly instead of passing it through .ps1 file ? 
    Thursday, April 11, 2019 12:17 PM
  • Best to use a file.


    \_(ツ)_/

    Thursday, April 11, 2019 12:54 PM