Batch Script which deletes all files out of the Recycle Bin - windows

I tried this but it says "no permission"
#echo off
echo %~dp0
rd /s c:\$Recycle.Bin
pause
I need help.
Please

I'm not sure if this is possible as a regular user:
Googling "empty recycle bin commandline" led me to this URL, advising following command:
cleanmgr /sageset:1
This will indeed empty the recycle bin, but the tool seems to be interactive. I asked help via following command:
cleanmgr /?
But this gave me no clue on how to do this without user interaction.

Having just run the script myself, it looks like it does succeed in deleting the contents of the Recycling Bin. I also got the same permissions error you did.
However, I ran it again later as administrator and not only was the Recycling Bin deleted but it also did not give the permissions error. So its just a permissions bug and otherwise everything should behave as expected! You can probably run this without elevated permissions and be just fine, if you don't mind seeing the permission denied message. Probably better safe than sorry here.
Note: I daily drive Linux and am by no means an expert with Windows. I am not sure how dangerous the script is, but be careful with it regardless since it can delete files with elevated permissions (if you wanted to get rid of the permission denied messages).

Thank You
This was helpfull
:))

Related

Windows %CD% environment variable not being updated

I have this simple batch file (test.bat):
echo %CD%
pause
No matter where I run this .bat file from (i.e. C:\some\dir\test.bat), my system tells me that I'm in C:\ . This is obviously neither the intended nor the desired behavior, and if I try the exact same script on a different machine, it behaves as expected, giving me current directory of the .bat file. I've been googling for hours now and haven't been able to find anything relevant to my issue. Perhaps I missed up a windows setting somewhere? I have no idea, anyone have any ideas?
Found the issue: there was an AutoRun registry entry that would change directory to C:\ whenever a shell was opened. facepalm

Batch administrator permission check

Good morning,
I'm trying to verify if a batch is executed with administrator permission.
I found this command to verify:
openfiles >nul 2>&1
if NOT %errorLevel% == 0
...
or net session instead of openfiles.
Everything works if I open command prompt as administrator and return an error if I use normal command prompt.
The problem appears when i try to execute two batch in a single elevated permissions command prompt;
The first batch is executed correctly, the second return permissions error.
what am i doing wrong?
It's not as compact as the method you're using, but another method I've successfully used numerous times in the past is as follows:
IF EXIST %SYSTEMROOT%\SYSTEM32\WDI\LOGFILES GOTO GOTADMIN
[Whatever commands you want to run if not running as admin]
:GOTADMIN
[Whatever commands you want to run if running as admin]
This works because, by default, Windows only allows access to the WDI directory with elevated permissions. Thus, by telling the script to look inside that directory for another directory that is supposed to exist, it can be used to detect whether it's running as admin or not. If yes, it'll see that LOGFILES exists and return True. If no, it won't be allowed access to the WDI files, therefore it won't see that LOGFILES exists and will return False.
Admittedly, this only works if the default security permissions on the WDI directory have not been changed, but from my experience it is highly unlikely these permissions would have been changed in most installations. It also requires that the LOGFILES directory inside of the WDI directory has not been deleted or renamed, but I find this highly unlikely to occur since it is an integral part of the WDI directory's purpose.
I have tested and confirmed this to work on Windows 7, 8, 8.1 and 10. I have not tested it on Vista, but I would assume it works because of how similar Vista is to 7. Obviously there's no benefit in using this method on XP since all command prompts by default are run at maximum elevation on that version of Windows.

Invalid number of parameters in Batch but not in cmd

I am trying to make a scheduled batch file to run with the volume shadow copy service to copy the outlook.pst file from microsoft outlook.
The commands work properly when i enter them in the command panel but when i setup the same code in a batch file it gives me an invalid number of parameters error in during the second line.
You have to use administrator access on cmd and the batch file for it to even work and i am doing that but i have no clue as to why i get an error only in the batch version but not in cmd?
I found this link helpful.
Its where i found the batch file which simplifies the process incase anyone is wondering.
Heres the code :
CALL MountLatestShadowCopy "C:\MyShadow\"
xcopy /y "C:\MyShadow\Users\%USERNAME%\Documents\Outlookove datoteke\*.pst" "\\hyp\backup"
RMDIR "C:\MyShadow"
pause
Anyone have any ideas as to the cause of my problems? Its supposed to simply copy the .pst file to a server for backup which i will run on a regular schedule.
Outlook is usually the first thing people open when they turn on their computers so i have to use volume shadow copy otherwise getting a copy might be hard on certain computers.
Thanks for the help in advance!

How can I mount VHDX file using powershell properly?

I'm trying to automate process of mounting a particular *.vhdx file to Windows Server 2012r2.
When I'm mounting file manualy by GUI everything works just fine. The problem occure when I'm mounting file using Mount-DiskImage Powershell CmdLet. At first glance mounting ends successfuly but when I trying to create a file inside disk V:\ (which is the mounted VHDX in my case) I got an error about lack of permissions. I was thinking that somehow it' caused by wrong owning of folders, files and the disk itself, so I run :
takeown /f V:\ /a /r /d Y
(...to reset all the permissions and grant them to Administrators.)
And what do you think? Yes. It didn't worked out. Then I checked SDDL string of V:\ in both cases of mounting VHDX (manually and with Powershell CmdLet) and it's turned out that they are absolutly the same (O:BAG:DUD:AI(A;ID;FA;;;WD)(A;ID;FA;;;BA)).
I can't find any reason why two of this methods give me different results. And I also can't find any visible difference in any properties of disk V:\ .
Do you have any suggestions? Any ideas would be helpfull!
Thanks in advance for your time guys.

Correct format of a UNC path for CACLS for a remote folder

I would like to list the ACLs on a file that is shared on a remote machine (I do have all the admin permissions)
It is available in \win2003ad\testfolder
When I run the command
cacls \\win2003ad\testfolder
I get the error "The network path could not be found." However, when I point out to a particular object in the shared folder, e.g.
cacls \\win2003ad\testfolder\users.csv
I get the correct result. Also, I get the correct result when I check it locally, i.e.
cacls c:\testfolder
Have you got any idea how this can be solved? (I can only use command line (no PS) and I run win2003
Quite old now, but no solution yet... cacls is an old tool, you should use icacls:
icacls \\win2003ad\testfolder
Just tried it on a Windows Server 2003, just in case the command was not there yet, and it worked.
I just solved this yesterday
cacls \\win2003ad\testfolder\*

Resources