Solved

Deleting folders with environment variables included in Powershell

Posted on 2014-01-15
5
286 Views
Last Modified: 2014-01-16
I need to delete certain folders when a user logs into their computer using powershell.

I am familiar with remove-item, but not sure how to use that using an environment variable.

I need to delete/wipe the following folders under

c:\users\%username%\my files
c:\users\%username%\my deltas
c:\users\%username%\my games
c:\users\%username%\my contacts

I appreciate the help, will be rewarded right away.

Thanks!
0
Comment
Question by:mystikal1000
5 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 400 total points
ID: 39782547
You want to delete folders under %UserProfile%, not under "c:\users\%username%".
Anyway, try
ForEach ($Folder In "My Files", "My Deltas", "My Games", "My Contacts") {
	$Path = Join-Path ${ENV:UserProfile} $Folder
	"Doing something with '$Path' ..."
}

Open in new window

0
 
LVL 10

Expert Comment

by:remmett70
ID: 39782562
The folders you want to delete are in the environment variable Usersprofile

remove-item $env:usersprofile\<foldername> -confirm:$false -recurse
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 39782673
Do I have to be a local admin to run the execution policy?

File H:\Powershell\Delete Folders\deletefolders.ps1 cannot be loaded because the executio
n of scripts is disabled on this system. Please see "get-help about_signing" for more det
ails.
At line:0 char:0
0
 
LVL 84

Assisted Solution

by:oBdA
oBdA earned 400 total points
ID: 39782785
Depends. If you want to set it for the system, you need an elevated prompt.
If you want to do it for a regular user (or an administrative user with UAC active in a non-elevated console), you can, for example, use
Set-ExecutionPolicy  -ExecutionPolicy RemoteSigned -Scope CurrentUser

Open in new window

0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 100 total points
ID: 39783041
It definitely makes sense to change that on the machine instead for the current user, so you have to set it up only once.
One way to execute the command elevated is:
Start-Process -Verb RunAs powershell -Argumentlist 'set-executionPolicy RemoteSigned'

Open in new window

It will ask for confirmation or admin password.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question