PowerShell Tutorial 33-40
PowerShell Tutorial 33-40
6 Renaming Files
The Rename-Item cmdlet enables you to change the name of an object while leaving its content intact. It’s
not possible to move items with the Rename-Item command; for that functionality, you should use the
Move-Item cmdlet as described above.
[system.enum]::getnames([System.Security.AccessControl.FileSystemRights])
If you’re not familiar with NTFS permissions management, check out this NTFS Permissions
Management Best Practice guide.
The PowerShell set-acl cmdlet is used to change the security descriptor of a specified item, such as a file,
folder or a registry key; in other words, it is used to modify file or folder permissions. The following script sets
the “FullControl” permission to “Allow” for the user “ENTERPRISE\T.Simpson” to the folder “Sales”:
33
$acl = Get-Acl \\fs1\shared\sales
$AccessRule = New-Object
System.Security.AccessControl.FileSystemAccessRule("ENTERPRISE\T.Simpson","FullControl","Allow")
$acl.SetAccessRule($AccessRule)
$acl | Set-Acl \\fs1\shared\sales
Note that the SetAccessRule parameter completely overwrites the permissions for a user or group, so you
can change folder permissions using this parameter. If you just want to add permissions, use the
AddAccessRule parameter instead. For instance, the following script adds the “FullControl” permission for
the “ENTERPRISE\J.Carter” user account to the “Accounting” folder:
Here are the other permissions you can assign to users or security groups:
Delete Delete
34
There are also sets of basic access rights that can be applied:
Access Rights Set Rights Included in the Set Name of the Set in PowerShell
Read Attributes
Read Permissions
Write Attributes
Read Attributes
Read Permissions
Read Attributes
Write Attributes
Delete
Read Permissions
35
To copy permissions, a user must own both the source and target folders. The following command will copy
the permissions from the “Accounting” folder to the “Sales” folder:
If you want to get a list of NTFS permissions via PowerShell, you can follow this easy how-to about
exporting NTFS permissions to CSV.
Note that RemoveAccessRule deletes only specific permissions. To completely wipe T.Simpson’s
permissions to the “Sales” folder, use the PurgeAccessRules command:
Note that PurgeAccessRules doesn’t work with a string user name; it works only with SIDs. Therefore, we
used the “Ntaccount” class to convert the user account name from a string into a SID. Also note that
PurgeAccessRules works only with explicit permissions; it does not purge inherited ones.
36
3.9 Enabling and Disabling Permissions Inheritance
NTFS permissions can be either explicit or inherited. Explicit permissions are permissions that are
configured individually, while inherited permissions are inherited from the parent folder. The hierarchy for
permissions is as follows:
Explicit Deny
Explicit Allow
Inherited Deny
Inherited Allow
The first parameter is responsible for blocking inheritance from the parent folder. It has two states:
“$true” and “$false”.
The second parameter determines whether the current inherited permissions are retained or removed.
It has the same two states: “$true” and “$false”.
Let’s disable inheritance for the “Sales” folder and delete all inherited permissions as well:
All inherited permissions were removed; only access permissions added explicitly are left.
Let’s revert this change and re-enable inheritance for the “Sales” folder:
37
3.10 Changing File and Folder Ownership
If you want to set an owner for a folder, you need to run the SetOwner method. Let’s make
“ENTERPRISE\J.Carter” the owner of the “Sales” folder:
Notice that we again used the Ntaccount class to convert the user account name from a string into a SID.
Note that the SetOwner method does not enable you to change the owner to any account you want; the
account must have the “Take Ownership”, “Read” and “Change Permissions” rights.
38
4. Automating PowerShell Scripts
Now let’s explore you how to create scheduled tasks using PowerShell scripts and Microsoft Windows Task
Scheduler.
In Windows Powershell 2.0 (Windows 7 or Windows Server 2008 R2), to create a scheduled job, you must use
the Task Scheduler module. Install the module by running the Import-Module TaskScheduler command,
and then use the following script to create a task that will execute the PowerShell script named
“GroupMembershipChanges.ps1” daily at 10 AM:
Windows PowerShell 3.0 and 4.0 (Windows Server 2012 R2 and above) don’t include the Task Scheduler
module, so this script will not work. Instead, PowerShell 3.0 and 4.0 include new cmdlets for creating
scheduled tasks, New-ScheduledTaskTrigger and Register-ScheduledTask, which make creating a
scheduled task much easier and more convenient. So let’s create a task that will execute our script daily at
10 AM using the system account (SYSTEM), which has elevated privileges:
39
Other trigger options that could be useful in creating new tasks include:
-Once — Triggers the task once. You can set a repetition interval using the –RepetitionInterval parameter.
Note that, it is not possible to trigger execution “on an event” using these cmdlets; PowerShell scripts with
“on an event” triggers are much more complicated. However, it is possible to do so with the Task Scheduler
tool, so this is a real disadvantage of using PowerShell rather than Task Scheduler.
To create a task, open Task Scheduler by pressing “Windows+R” and typing taskschd.msc in the window that
opens. Then take the following steps:
1. Click Create a task and enter a name and description for the new task. To run the program with
administrator privileges, check the Run with the highest privileges box. In our example, we’ll assign a
service account to run the task and run it regardless of whether the user is logged on.
40