0
votes

so i am writing a script, in our company we store users home-directory on network drives, and when they leave we rename the directory by adding .left to the folder name, example: "name.left" and we actually used to do so by finding the user in AD copying the content of the home directory property and renaming it. so i got this so far :

$name = Read-Host 'User Name: '
$path = Get-ADUser $name -Properties homedirectory
Rename-Item $path {$name+".left"}

problem is when i get the home-directorys path in get-aduser it gives it with the standart get-aduser output, it just adds it to the output so i tried using filter :

$path = Get-ADUser $name -Properties homedirectory -Filter homedirectory 

and it gave me an error, but not for the filter, now it doesn't recognize the user name i gave it.

now, I'm sure that there is a way to filter the string in the property. and i get a feeling that the third line i wrote might be wrong as-well, but that's my python brain trying to work with powershell :) so if enyone could help me with that one, i would really appreciate it, and if anyone can point me to good powershell guides that would be really nice.

EDIT: so i fixed it to look like that :

$name = Read-Host 'User Name: ' $date = Read-Host 'Date Please: ' do { $path = Get-ADUser $name -Properties homedirectory | select -Expand HomeDirectory $newname = {$name + "LFT" + $date} Rename-Item $path $newname Write-Host $name + 'changed' } while ($name -ne 'exit')

and i get an error that the new name is a script and not a string so it cant run, you know a way to fix it?

Rename-Item : Cannot evaluate parameter 'NewName' because its argument is specified as a script block and there is no input. A script block cannot be evaluated without input. At line:6 char:19 + Rename-Item $path $newname + ~~~~~~~~ + CategoryInfo : MetadataError: (:) [Rename-Item], ParameterBindingException + FullyQualifiedErrorId : ScriptBlockArgumentNoInput,Microsoft.PowerShell.Commands.RenameItemCommand

EDIT2 a fellow employee helped me out and this is the result: it does exactly what i needed :)

$name = Read-Host 'User Name '
$homefolder =  (Get-ADUser $name  -Properties HomeDirectory).homedirectory
$date =  Get-Date -UFormat "%d.%m.%Y"
ren $homefolder -newname ($homefolder + "_lft_" + $date)
1

1 Answers

0
votes

With select-object you can filter out unwanted properties. It will still return an object tho. It has a handy argument named expand that will return single properties value:

$path = Get-ADUser $name -Properties homedirectory | select -Expand HomeDirectory