# this script checks to see if the folder has already been created
# if it hasn't it will create the folder then sets permissions on it
# then verifies if the folder has been created or not
# NEED TO CHANGE PATH ($Path = '\\server\folder\folder') !!!
# NEED TO CHANGE USER PERMISSION ($permission = 'UserAliasGoesHere') !!!
# this is the path for the new folder
$Path = '\\server\folder\folder'
# test to see if folder already exists
if (Test-Path $Path) {
Write-Host -ForegroundColor Yellow "
-------------------------------------------------------
`n
The folder has previously been created. No action taken
`n
-------------------------------------------------------
"
}
else {
# create new folder
$null = New-Item -Path $Path -ItemType Directory
# get permissions
$acl = Get-Acl -Path $path
# add a new permission (FullControl, Modify, Read)
$permission = 'UserAliasGoesHere', 'Modify', 'ContainerInherit, ObjectInherit', 'None', 'Allow'
$rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $permission
$acl.SetAccessRule($rule)
# set new permissions
$acl | Set-Acl -Path $path
# verify the folder has been created
if (Test-Path $Path) {
Write-Host -ForegroundColor Green "
----------------------------
`n
The folder has been created.
`n
----------------------------
"
}
else {
Write-Host -ForegroundColor Red "
--------------------------------
`n
The folder has not been created.
`n
--------------------------------
"
}
}
# this is the path for the new folder
$Path = Read-Host 'What is the folder path?'
# this is the users SAM account (alias) This is who will have modify rights to the folder
$alias = Read-Host 'What is the user alias?'
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented:
* Package this in a function
* Add a parameter, so the user can pass a path for the file to be created.
* Add a standard documentation block that will show up when someone does a help/get-help
* (optional) Add a parameter that would allow the invoking code to pass ownership credentials for the newly created file.