Link to home
Create AccountLog in
Windows OS

Windows OS

--

Questions

--

Followers

Top Experts

Avatar of Sam Simon Nasser
Sam Simon Nasser🇵🇸

Powershell rename-item, Access denied.
Dears,
I have a strange problem in Powershell. I'm making a small script to rename a folder (found in all users profile) to rename it to their name. below is the script.
$localPath = "c:\Users\$env:UserName\Documents\Adobe\Premiere Pro\13.0\Profile-setup"

if([System.IO.directory]::Exists($localPath)){
Rename-Item -force -Path $localPath -NewName "Profile-$env:UserName" 
}

Open in new window


I worked in Powershell ISE, and the script WORKES PERFECT.
when I try to run the PS1 file(using local GPO, Task Scheduler, Startup, batch files), it would not rename it. I set the execution policy to unrestricted
today when investigating more when I open Powershell or CMD (not PowerShell ISE) with administrator privileges and  try to run it using:
powershell -File c:\PS\rename.ps1

Open in new window

it gives me the following error
C:\powershell -File c:\PS\rename.ps1
Rename-Item : Access to the path 'C:\Users\profilesetup\Documents\Adobe\Premiere Pro\13.0\Profile-setup' is denied.
At C:\PS\rename.ps1:7 char:1
+ Rename-Item -force -Path $localPath\Profile-setup -NewName $env:UserN ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\Users\profil...0\Profile-setup:String) [Rename-Item], IOException
    + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

Open in new window

although i'm an in admin account, admin PS and have full access to the folder. As mentioned before, the scipt run using ISE but not in command PS.

Any ideas? OS is Windows 10 Pro Educational

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of QlemoQlemo🇩🇪

Might happen that a process has a handle open for the path or file to rename, and that it worked in ISE has been nothing but luck.

Avatar of Michael B. SmithMichael B. Smith🇺🇸

This (from the error):
Rename-Item -force -Path $localPath\Profile-setup -NewName $env:UserN ...

Open in new window

doesn't match this (from the script you pasted):
Rename-Item -force -Path $localPath -NewName "Profile-$env:UserName"

Open in new window


What's the actual code?

Avatar of Chirag NagrekarChirag Nagrekar🇮🇳

Are you running powershell with Administrator. (Right click and run as administrator) ?

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Sam Simon NasserSam Simon Nasser🇵🇸

ASKER

@chirag: admin account, and admin PowerShell
@michael: to be clear; each user log in the domain has a folder name Profile-setup (c:\Users\$env:UserName\Documents\Adobe\Premiere Pro\13.0\Profile-setup), the Profile-setup must be renamed to his name (profile-sam for instance). as i mentioned, running the script
Rename-Item -force -Path $localPath -NewName "Profile-$env:UserName"

Open in new window

has no problem in ISE, but does not run in PS. I even tried the x86 and x64 of PS with the same result.

Avatar of Sam Simon NasserSam Simon Nasser🇵🇸

ASKER

update; strange thing!
the code works! I tried in many accounts, and it just works. but I ran it manually. I will try to deploy on startup or GPO and feedback

Avatar of Sam Simon NasserSam Simon Nasser🇵🇸

ASKER

update:
if i run PowerShell in CMD windows (as .cmd or .bat), i gives me the error (see image - part 1). If I go directly to PowerShell window, run the script, it will work (part 2)
User generated imageif there is any other suggestions or code change, I don't mind. I just need the folder renamed at user log on!

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Sam Simon NasserSam Simon Nasser🇵🇸

ASKER

update: run the script on Windows 7, like a charm. so I guess it windows 10 or PowerShell 5 that is causing the issue?
PowerShell code
 $localPath = "c:\Users\$env:UserName\Documents\Adobe\Premiere Pro\13.0\Profile-setup"

if([System.IO.directory]::Exists($localPath)){
#Get-Item -Path "c:\Users\$env:UserName\Documents\Adobe\Premiere Pro\13.0\Profile-setup" 
Rename-Item -force -Path $localPath -NewName Profile-$env:UserName 
}

Open in new window

batch file code
@ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%rename.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File """"%PowerShellScriptPath%"""" ' -Verb RunAs}"
pause	

Open in new window

you can test it on your machines by creating the folders (Adobe\Premiere Pro\13.0\Profile-setup) in your documents folder. one you run the script, it should be renamed from profile-setup to profile-yourusername

Avatar of Michael B. SmithMichael B. Smith🇺🇸

Change -Path to -LiteralPath.

Avatar of Sam Simon NasserSam Simon Nasser🇵🇸

ASKER

@michael, tried that already, same result.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of QlemoQlemo🇩🇪

It works on my W10 1809 with PS 5.1 running the cmd file in a admin prompt.

I recommend  to run SysInternals ProcMon with a filter of "path", "contains" , "profile-" (or more restrictive), and only the file monitoring enabled, then run the script and see what is reported.

Avatar of Sam Simon NasserSam Simon Nasser🇵🇸

ASKER

I will test on Monday when I get back to work

Avatar of Sam Simon NasserSam Simon Nasser🇵🇸

ASKER

No correct answer was provided. I ended up using robocopy

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of Sam Simon NasserSam Simon Nasser🇵🇸

ASKER

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account
Windows OS

Windows OS

--

Questions

--

Followers

Top Experts

This topic area includes legacy versions of Windows prior to Windows 2000: Windows 3/3.1, Windows 95 and Windows 98, plus any other Windows-related versions including Windows Mobile.