PowerShell Tutorial 25-32
PowerShell Tutorial 25-32
If you want to copy all members from one group to another group, run the following script:
To remove a computer account from a group, specify the computer name with a dollar sign ($) at the end as
the value for the -Members parameter.
An easy way to remove multiple users from an AD group is to create a CSV file with the list of usernames and
then remove those users from the group object using this script:
25
Note that the user will lose all group membership except Domain Users, which can be removed manually if
needed.
Don’t forget to enable the Active Directory Recycle Bin feature so you can easily roll back your changes if
something goes wrong.
Use the same syntax to move computer objects. The following command will move the computer “R07GF” to
the “Computers” container:
If you have a predefined list of objects to move, you can save it as a CSV file and then import that file to Active
Directory. The CSV list should be in the following format:
26
Use this PowerShell script to move AD user accounts listed in a CSV file:
$Imported_csv | ForEach-Object {
# Retrieve DN of user.
$UserDN = (Get-ADUser -Identity $_.Name).distinguishedName
# Move user to target OU.
Move-ADObject -Identity $UserDN -TargetPath $TargetOU
}
To move AD computer accounts listed in a text file, use the following PowerShell script:
# Specify path to the text file with the computer account names.
$computers = Get-Content C:\Temp\Computers.txt
27
3. Top 10 File System Management Tasks
Using PowerShell
Every day, system administrators have to perform a range of standard operations on the numerous files and
folders on their Windows servers, from managing user data on shared resources to maintaining backups
properly. Using the following information, you can automate many of these tasks and save time for more
important projects.
In this part, we explain how to automate file management and NTFS permissions management tasks with the
help of PowerShell scripts.
If you want to also check all subfolders and their content, add the -Recurse parameter:
To filter the output, add the Filter, Exclude, Include and Path parameters to the Get-ChildItem cmdlet. For
advanced object filtering, use the Where-Object cmdlet. The script below searches for all executable files in
the IT folder that were modified after April 1, 2018:
28
3.2 Creating Files and Folders
To create new objects with Windows PowerShell, you can use the New-Item cmdlet and specify the type of
item you want to create, such as a directory, file or registry key. For example, this command creates a folder:
If you need to create a file and write data to it, there are at least two built-in methods. The first is to use the
Out-File cmdlet:
Alternatively, you can create files using the Export-Csv cmdlet, which exports the output to a csv file that can
be opened in Excel:
29
If you have already made sure that every object inside the folder should be deleted, you can use the
-Recurse switch to skip the confirmation step:
Sometimes you need to clean up old files from a certain directory. Here’s the way to accomplish that:
$Folder = "C:\Backups"
30
Here’s how to check whether a file exists and delete it if it does:
$FileName = 'C:\data\log.txt'
If (Test-Path $FileName){
Remove-Item $FileName
}
To delete files from remote PCs, you must have the appropriate security permissions to access them.
Be sure to use UNC paths so the script will correctly resolve the file locations.
If the target file already exists, the copy attempt will fail. To overwrite the existing file, even if it is in
Read-Only mode, use the -Force parameter:
If you’re copying files to or from remote computers, be sure to use UNC paths. For example, use this
command to copy files from a remote file server to the local C: directory:
31
To copy files from your local directory to the remote folder, simply reverse the source and destination
locations:
You can also copy files from one remote server to another. The following script recursively copies the
\\fs\Shared\temp folder to \\fs\Shared\test:
To copy only certain files from the source content to the destination, use the -Filter parameter. For instance,
the following command copies only txt files from one folder to another:
You can also run the XCOPY and ROBOCOPY commands to copy files, or use COM objects as in the example
below:
The following command moves a specific backup file from one location to another:
This script moves the entire Backups folder and its content to another location:
The Backups directory and all its files and subfolders will then appear in the archive directory.
32