Solved

adding credentials to a powershell command

Posted on 2014-12-31
2
115 Views
Last Modified: 2015-01-14
I am trying to delete some files on a network share.  I need to add different credentials in the script of an administrator to finish the command.
can I get the exact syntax of the powershell command if these are my variables.
these are made up examples of variables....

server unc path
\\apples\stems\*.*
domain = trees
username = moon
pwd = rock

I want to delete all files within the directory of \\apples\stems

also for bonus points... if you could tell me how to delete all the files in that directory that are over 14 days old

so two scripts requested.
Thanks!
0
Comment
Question by:jamesmetcalf74
2 Comments
 
LVL 24

Accepted Solution

by:
DMTechGrooup earned 500 total points
ID: 40525564
Why not run the script as a user with privileges

http://www.powershellmagazine.com/2012/11/09/pstip-start-powershell-as-a-different-user-with-elevated-privileges/

For files older than x days..

$limit = (Get-Date).AddDays(-15)
$path = "C:\Some\Path"

# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40530250
The admin should have access to that folder, so why changing identity? It is difficult to connect to a share with a different user if there is a connection to any share on that server from this session, so it is something to avoid.
0

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Synchronize a new Active Directory domain with an existing Office 365 tenant
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scannerā€¦
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

726 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