Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

adding credentials to a powershell command

Posted on 2014-12-31
2
Medium Priority
?
130 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 24

Accepted Solution

by:
DMTechGrooup earned 2000 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 71

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

609 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