Copy file to all users in a Specific OU - powershell

I want to copy a file to all users in a specific OU using powershell (My knowledge is not great and have just tried amending stuff I have found on google)
So this is what I have tried
$Source = '\\\FS1\D$\Component 3 Skills log template.docx'
$users = Get-ADUser -Filter * -SearchBase 'OU=Drama,OU=ComputerBasedExams,OU=TAW100STUDENTS,OU=TAW100,DC=something,DC=co,DC=uk'
$Destination = '\\\FS1\\Homes\taw100students\'
foreach ($i in $users){
{Copy-Item $Source -Destination $Destination\\$i -Recurse}
I do not get any error, but the files do not copy either

If you aren't sold on using powershell for this i would recommend using Group Policy to push files to a group of user or machines.
Here's a good tutorial on how to use gpo:
http://woshub.com/copy-files-on-all-computers-group-policy/

I had some help elsewhere and this code worked
$Source = '\\FS1\D$\Component 3 Skills log template.docx'
$Destination = '\\FS1\Homes\taw100students'
$sb='OU=Drama,OU=ComputerBasedExams,OU=TAW100STUDENTS,OU=TAW100,DC=something,DC=co,DC=uk'
Get-ADUser -Filter * -SearchBase $sb |
ForEach-Object{
$dest = "{0}\{1}" -f $Destination, $_.Name
Copy-Item $Source -Destination $dest
}

Related

PowerShell - Double loop possible?

I want to compress a directory in a specific place.
The source path is : \\$Computers\Users\$Names
I want than for each computers a copy of each users directory in the sources path of each computers
I tried to use a foreach loop like :
$Computers = Get-ADComputer -Filter "Name -like 'PC*'" | Select-Object -ExpandProperty Name
$Names = Get-aduser -filter * | Select-Object -ExpandProperty givenname
Foreach($Computer in $Computers)
{
Compress-Archive -Path \\$Computer\Users\* -DestinationPath C:\Saves\\$Computer\Test.zip -Force
}
This actually work, but I don't know how can I add a second loop inside the loop.
If anyone can just explain me the function or just some advises please for trying to do that.
Thank you for your time.
You're approaching the problem with the wrong logic, you do need an inner loop, however, instead of attempting to compress a user profile that you don't know for sure is there you can instead query the remote computer's Users folder to see which ones are there and compress only those ones:
$Computers = (Get-ADComputer -Filter "Name -like 'PC*'").Name
# Add the profiles you want to exclude here:
$toExclude = 'Administrator', 'Public'
$params = #{
Force = $true
CompressionLevel = 'Optimal'
}
foreach($Computer in $Computers)
{
$source = "\\$Computer\Users"
Get-ChildItem $source -Exclude $toExclude -Directory | ForEach-Object {
$params.LiteralPath = $_.FullName
# Name of the zipped file would be "ComputerExample - UserExample.zip"
$params.DestinationPath = "C:\Saves\$computer - {0}.zip" -f $_.Name
Compress-Archive #params
}
}

Powershell, Get access rights of shared files, format output?

I have some shared files set up for me for testing purposes, on a Windows Server 2016.
My given task is to get all the users, and their access rights to there shared files/folders.
I get the shared files with
Get-SmbShare | Select-Object -Property Name, Path
What I think I should do, is passing each share's path into
Get-Acl
So I came up with this:
$shares = Get-SmbShare | Where-Object Name -notlike "*$" | Select-Object Name
foreach ($share in $shares){
$path = "\\$env:COMPUTERNAME\" + $share.Name.ToString()
$FolderPath = dir -Directory -Path $path -Recurse -Force
Foreach ($Folder in $FolderPath) {
$Acl = Get-Acl -Path $Folder.FullName
foreach ($Access in $acl.Access)
{
$Folder.FullName;
$Access.IdentityReference;
$Access.FileSystemRights;
$Access.IsInherited
}
}
}
My question is: How could I format this output, so it looks readable, and/or is there a simpler, maybe cleaner to do what I intend to do?

Powershell Script - Find the a list with path of the password protected .xlsx AND .xls files in a network folder

I am currently working on finding a way to get a list with path of all the .xlsx and .xls file that are password protected in a network drive that contains tons and tons of folders and sub folders. I put together this script below that works fine, but it only returns .xlsx files, none of the .xls files with password protected were returned. I am wondering if anyone knows how to get the .xls file with password or any other script that would get this job done? Appreciate all your help!
Script
$path = "C:\Users\DC\Desktop"
$dest = "C:\Users\DC\Desktop\ExcelWithPassword.txt"
$Full = Get-ChildItem $path -Include *.xlsx*, *.xls* -Recurse -ErrorAction SilentlyContinue
$List = select-string -pattern "<encryption" $Full
foreach ($file in $List) {
$file.path | Out-File $dest -Append -Force
}
The output is basically a list of paths where those password protected files are located.
unless you have other files in the target directory tree, with an '.xl extension. Why are you doing this ...
Get-ChildItem $path -Include *.xlsx*, *.xls* -Recurse -ErrorAction SilentlyContinue
... you only need this...
Get-ChildItem $path -Include *.xl* -Recurse -ErrorAction SilentlyContinue
If you are after just the full path, ask for it, using this ...
Get-ChildItem $path -Include *.xl* -Recurse -ErrorAction SilentlyContinue |
Select-Object -Property Fullname
# Results
<#
FullName
--------
D:\Temp\NewFolder\Test.xlsx
D:\Temp\Test.xls
D:\Temp\Test.xlsx
#>
... or this.
(Get-ChildItem $path -Include *.xl* -Recurse -ErrorAction SilentlyContinue).FullName
# Results
<#
D:\Temp\NewFolder\Test.xlsx
D:\Temp\Test.xls
D:\Temp\Test.xlsx
#>
As far as the loop, you can also shorten your code to something similar.
(Get-ChildItem $path -Include *.xl* -Recurse -ErrorAction SilentlyContinue |
Select-Object -Property Fullname) -match '<encryption' |
Out-File $dest -Append -Force
Or
(Get-ChildItem $path -Include *.xl* -Recurse -ErrorAction SilentlyContinue).FullName -match '<encryption' |
Out-File $dest -Append -Force
You are not saying how the files were encrypted. Excel allows for protecting the sheet, the workbook, etc. You can't check a password-protected file by searching for a string without opening the file. To open the file you must use the application interface to open the file. For Excel it's:
### Automate Excel
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$workbook1 = $excel.Workbooks.Add()
$Sheet = $Excel.WorkSheets.Item(1)
$Sheet.Cells.Item(1,1) = "Hello from Powershell "
$Sheet.Cells.Item(1,2) = "Using VBA from Excel Button object"
Based on what you are after, there are a few other considerations you must have. Scanning and doing this across the whole network and thousands of files requires planning, and parallel processing.

Search AD users for a user

I have a query regarding searching AD.
I have written this piece of code for moving HomeDrives of users which does not have an AD account in the AD.
Get-ChildItem -LiteralPath "\\server1\path" -Force |
Where-Object {$_.PSIsContainer} |
ForEach-Object {
$Name = Split-Path -Path $_ -Leaf
$ADResult = ([adsisearcher]"(samaccountname=$Name)").Findone()
if (!($ADResult)) {
$sNewPath = "\\server1\newpath"
Move-Item -Path $_.Fullname -Destination $sNewPath -Force
}
}
The thing is that I want to run this from another server and the AD is on another server.But this:
$ADResult = ([adsisearcher]"(samaccountname=$Name)").Findone()
will run only if AD is on this server.
So I want to replace this line with a solution that can access AD which is on server2.
Can I use Get-ADUser or Search-ADAccount to achieve this?
I use:
Get-WmiObject Win32_UserAccount
You can specify the username with a filter.
Get-WmiObject Win32_UserAccount -filter 'name="username"'

Delete multiple files or folders from a CSV file that contain more than one columns (Powershell)

I need some help with script that will delete AD Disabled users Home Folders and Roaming Profiles folders on the Server (DC).
Steps That I already done, I create a powershell command:
Import-Module ActiveDirectory
Get-ADUser -SearchBase "OU=Marked for Deletion,OU=Disable Users,DC=******,DC=com" -Filter * -Property * |
Select-Object -Property homeDirectory,profilePath | Export-CSV -Path .\Remove.csv
This Command export the properties of home folders and roaming profile folders of disabled users.
Now' the CSV file contains two colmuns, one is "homeDirectory" and second "profilePath"
The Problem is, when i execute this script, i get error.
$folders = Get-Content "C:\lab\remove.csv"
foreach ($homeDirectory in $folders) {
Remove-Item -Path $homeDirectory -force -Recurse
}
foreach ($profilePath in $folders) {
Remove-Item -Path $profilePath -force -Recurse
}
write-host -foregroundcolor yellow "Delete action complete"
Can somebody help me with this, I will appreciate it.
First I would remove the type information from your CSV like so:
Import-Module ActiveDirectory
Get-ADUser -SearchBase "OU=Marked for Deletion,OU=Disable Users,DC=******,DC=com" -Filter * -Property * |
Select-Object -Property homeDirectory,profilePath |
Export-CSV -Path .\Remove.csv -NoTypeInformation
Then for your delete code I would use this:
Import-Csv "C:\lab\remove.csv" | % {
Remove-Item -Path $_.homeDirectory -force -Recurse
Remove-Item -Path $_.profilePath -force -Recurse
}
write-host -foregroundcolor yellow "Delete action complete"
The problem with your code is that you are not looping through a column, you looping by line and then doing it twice. To do it your way you would need to split the line at the comma.