Which user has this file open? Automated - powershell

I have a list of about 1000 PSTs that do not have an active directory owner assigned, nor is the folder, or file name identifiable to a user.
I would like to know what user has this file open so that I can identify the owner.
Given a list of file names.
C:\asdf.pst
C:\fdsa.pst
And a computer.
Murigar
I believe one could use OpenFiles.exe to produce a list of all open files and then extract this information.

When a user opens a PST file it creates a temporary file using a filename like ~asdf.pst.tmp. You can check the owner of that file to see who has the PST open.
Get-ChildItem "\\server\folder\*.pst.tmp" -Force | ForEach-Object {
$acl = Get-Acl -Path $_.FullName
$owner = $acl.Owner
$file = $_.Name.Replace("~","").Replace(".tmp","")
"$file is currently open by $owner"
}

Related

PowerShell create folder and set ACL

I have a need to create a user folder for about 500 users and rather than do it manually i would like to try using PowerShell. I have an csv file with a header called folder, this holds the username i.e. tuser which is what the folder should be named once created and that username should be added to the folder ACL with modify permissions in addition to inheriting the root permissions. I tried the script below but it did nothing not even errors. Any suggestions on how to best achieve this? The end result should be a new folder \fileserver\share$\Test\tuser
location where the folders will be created
Set-Location \fileserver\share$\Test
csv file with folder names
$Folders = Import-Csv C:\Temp\Scripts\newusers.csv
ForEach ($Folder in $Folders) {
New-Item $Folder.name -itemtype directory
As per my comment. I'd suggest trying something like this.
Import-Csv -Path 'C:\Temp\Scripts\newusers.csv' |
ForEach-Object {New-Item -Path '\fileserver\share$\' -Name $PSitem -ItemType Directory -Force}

Archiving user folders with powershell

I am currently working on a project with powershell to help clean up and save space on our server. I work in a secondary school and we have over 1000 users at our location. I have created a script to create a folder for each user in a location and give only that user and myself access to the folder for them to store their work and general documents on our NAS.
The problem I am going to be running into in the future though, is that I don't have a way of archiving their folders yet when the student leaves the school, so in a few years time there is going to be an issue of having only 1000 users, but 2000+ personal folders created, many of which can be archived for a period of time and then deleted to save space on the NAS.
The script I have created to generate their folders is below (I have redacted the AD group names and server locations for privacy)
Import-Module ActiveDirectory
Import-Module NTFSSecurity
$ADUsers = Get-ADGroupMember -Identity *user AD Group*
ForEach ($ADUser in $ADUsers)
{
New-Item -ItemType Directory -Path "*Server location*\$($ADUser.sAMAccountname)"
$userfolder = "*Server location*\$($ADUser.sAMAccountname)"
Get-Item $userfolder | Disable-NTFSAccessInheritance
Get-Item $userfolder | Add-NTFSAccess -Account $ADUser.sAMAccountname -AccessRights FullControl
Get-Item $userfolder | Remove-NTFSAccess -account *user AD Group* -AccessRights FullControl
}
This works fine for the folder creation, but I am trying to find a way to archive the user folders of students that have left. I have an idea of creating a CSV file by getting the current usernames from the AD group, then comparing them with the folders in the directory created by the script and have all matching folders stay, but all folders that don't appear in the csv file to be moved to another location for archiving however I am not sure if this is the best way to do it or if I am overlooking a solution that is already in place for this type of thing. Getting a list of users that have left is difficult because they just disappear from the system, I just have a list of current users.
I am currently trying to do this using CSV files, my thinking is to do something like this..
Get-ADGroupMember -Identity *user AD Group* | Select-Object samaccountname | Export-Csv -Path "*server location*\user test csv.csv"
Get-ChildItem "*server location*" | Select-Object PSChildName | Export-Csv -Path "*server location*\folder list.csv"
New-Item -ItemType file *server location*\combined_files.csv –force
Get-Content "*server location*\user test csv.csv", "*server location*\folder list.csv" | Add-Content *server location*\combined_files.csv
The above script creates a CSV file of user's SamAccountNames and a CSV file of folder names that were created by the first script and merges the two CSV files together, creating a new csv file that looks like
a
a
b
c
c
d
But I can't figure out how to remove all entries that are duplicated to leave just the unique entries so the new CSV looks like this
b
d
So that I can use this new CSV file to move the all the folders contained within to the new folder location for archiving.
Is my thinking correct that this is the best way to do this? or is there another better way to skin this cat?
So I have managed to figure out a solution to what I wanted to do and I have posted the script below for anyone else looking for a way to solve the problem.
The basic logic is this
Create a CSV file of the users that exist in AD
Create a CSV file of the folders that have been created over time
Compare the 2 files together and remove the current users from the list of folders leaving you with a list of folder names that belong to people who have left the site and save as a text file
A little clean up by removing the 2 CSV files that were generated to create the txt file
Do some editing to the txt file to remove the quotation marks that are generated from the formating of the CSV's
Create a new directory for archiving purposes if you don't already have a suitable location
Loop through the folders and move the folders with the corresponding usernames from the txt file to the new location
I have redacted server locations, adgroups etc but the script will still work once you put your information in there.
#This creates a CSV file of the all the users that are a member of the AD Group
Get-ADGroupMember -Identity *ADGroup* | Select-Object samaccountname | Export-Csv -Path "*CSV File Location*"
#This creates a CSV File of all the folders that have been generated over time for the use of a personal drive
Get-ChildItem *Server location* | Select-Object PSChildName | Export-Csv -Path "*CSV File Location*"
#This compares the 2 CSV files together, and removes names in the current user list CSV from the Current User Folder list CSV
#and creates a Text file that only contains the names of the folders of users who are no longer in AD and are assumed to have left the site
$disabledUsers = Get-Content -Path "*CSV File Location*"
$enabledUsers = Get-Content -Path "*CSV File Location*" | foreach {
if ($_ -notin $disabledUsers) { $_ }
}
Set-Content -Path "Text File location" $enabledUsers
#This is just to perform a little clean up of the csv files as they are no longer needed
Remove-Item -Path "*CSV File Location*"
Remove-Item -Path "*CSV File Location*"
#This removes the quotations that are created from converting the CSV files to a text file
(Get-Content *Text File location* -Encoding UTF8) | ForEach-Object {$_ -replace '"',''} | Out-File *Text File location* -Encoding UTF8
#This creates the new folder to store the user folders for archiving
New-Item -ItemType Directory -Path "*New Archive Folder Location*"
#This is the loop that then goes through the text file that contains all the users that no longer exist in the system
#and moves their folders to the archive location
$Userlist = Get-Content *Text File location* -Encoding UTF8
ForEach ($user in $Userlist)
{
Move-Item *server Location*$User -Destination *Archive Location*
}

To copy file with file name "abc.txt" to destination

I have a scanario where I need to copy file called "abc.txt" from its locations which is not known to destination location "C:\Temp".
We need to search "abc.txt" file and then copy the file to "C:\Temp"
Here source location is unknown and only the file name is known which is to be copied. Can we make use of current directory for this? If yes how can we ?
If I have understand the requirements correctly, you need to find a file in the current folder with the name "abc.txt" and, if the file exist
$AllFiles = Get-ChildItem -Recurse -Name "abc.txt"
foreach ($File in $AllFiles)
{
Write-Host $File
Copy-Item -Path $File -Destination "C:\Temp\"
}
If you want to look for the file in the current Folder AND the subfolders, use the "-Recurse" parameter, see Documentation of Get-Childitem:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7

Copy data for each user using PowerShell

I have a text file with a list of user names separated by semi colon, users names in the text file are: user1; user2; user3, etc.. The user names all have a network folder located at \testserver\users\user1, \testserver\users\user2, and so on.
I am trying to have PowerShell script read the text file and copy the folder and all data in each folder for each user from one location to another location called \testserver\newusers\users. However when I launch the script I have written so far, it just creates a folder with a user name from the text file I have. Below is what I have so far:
$File = Get-Content .\MyFile.txt
$File | ForEach-Object {
$_.Split(';') | ForEach-Object {
Copy-Item -Path "$_" -Destination '\\testserver\newusers\users'
}
}
I am launching my PowerShell .ps1 file from a location that has the myfile.txt file in it.
How do I get this to work properly?
Call Copy-Item with the parameter -Recurse if you want to copy the folders' content as well. Otherwise just the folder itself would be copied (without content). You also need to provide the full path to the source folders unless you run the script from \\testserver\users.
Something like this should work:
$server = 'testserver'
$src = "\\$server\users"
$dst = "\\$server\newusers"
(Get-Content .\MyFile.txt) -split ';' | % {
Copy-Item -Path "$src\$_" -Destination "$dst\" -Recurse
}

Read text file and run a copy command for each line in the text file

I am trying to write a Powershell script that will read a text file on my desktop that is filled with user names, then go out to a specified folder on our network share, lets say u:\data and copy the contents from that folder to another network share lets says y:\information, for each user in the text file.
How would this be written?
I have tried several things with reading the text file then trying several commands to copy and paste but they each failed.
UPDATE:
Below is what I have done so far:
$user = Get-Content "test.txt"
$path = "\\abnas2\abusers\users"
$path2 = "\\abnas2\abdept\dept\testcopy"
$Copy = Copy-Item -path $path\$user\ * -Destination $path2\$user
I had one username in the test.txt file called user1 and it pulled the name, and copied perfectly.
Now if I add more than one name to the test.txt file and run the above, it errors out. The error it returned made it look like the 3 user names in the list were one user name.
What I need this to do is run the command for each name on the list. I was thinking I could use the foreach command But not sure how to do it.
UPDATE - 04\09\2014:
I have tried the following and am getting an error back:
$user = Get-Content "test.txt"
$path = "\abnas2\abusers\users"
$path2 = "\abnas2\abdept\dept\testcopy"
$Copy = Copy-Item -path $path\$user* -Destination $path2\$user
foreach($username in $user) {
Copy-Item -path $path\$username* -Destination $path2\$username\
}
When I run it I am getting the following error:
Copy-Item : An object at the specified path \\abnas2\abusers\users\user1 user2 user3 does not exist.
These are the names in my test.txt file, is there a way to get it to read one line at a time and execute the copy and when done go to the next name on the list and do the same? I'm not sure how to get it to do that.
You can use foreach
In this case:
foreach($username in $user) {
Copy-Item -path $path\$username\* -Destination $path2\$username\
}
would copy the contents of each named folder in $user under $path to its corresponding folder in $path2.