What is problem-take ownership and delete a file?

This topic contains 1 reply, has 2 voices, and was last updated by  Don Jones 3 years, 4 months ago.

  • Author
    Posts
  • #14632

    Keith Moffitt
    Participant

    # Get the ID and security principal of the current user account
    $myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
    $myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)

    # Get the security principal for the Administrator role
    $adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator

    # Check to see if we are currently running "as Administrator"
    if ($myWindowsPrincipal.IsInRole($adminRole))
    {
    # We are running "as Administrator" -

    }
    else
    {
    # We are not running "as Administrator" - so relaunch as administrator

    # Create a new process object that starts PowerShell
    $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";

    # Specify the current script path and name as a parameter
    $newProcess.Arguments = $myInvocation.MyCommand.Definition;

    # Indicate that the process should be elevated
    $newProcess.Verb = "runas";

    # Start the new process
    [System.Diagnostics.Process]::Start($newProcess);

    # Exit from the current, unelevated, process
    exit
    }

    # Run your code that needs to be elevated here
    #takeown of files
    takeown /f c:\FA30-Win7-setup.exe /d y
    icacls c:\FA30-Win7-setup.exe /grant username:F /q

    #takeown of folders
    takeown /f c:\Users\Public\Documents\FaFiles30 /r /d Y
    icacls c:\Users\Public\Documents\FaFiles30 /grant username:F /t /q

    takeown /f c:\Program Files (x86)\FinAnal30 /r /d Y
    icacls c:\Program Files (x86)\FinAnal30 /grant username:F /t /q

    Remove-Item c:\FA30-Win7-setup.exe

  • #14648

    Don Jones
    Keymaster

    I'm sorry, but we're not going to be able to run your code and reproduce your results. If you're encountering an error or something, you're going to have to help us understand what that is.

    Although if the problem is solely with TAKEOWN or ICACLS, understand that those aren't actually PowerShell – they're external commands.

You must be logged in to reply to this topic.