# Script powershell

**URL:** <https://forums.powershell.org/t/script-powershell/10533>\
**Category:** Pester\
**Created:** [April 18, 2018, 6:27am UTC](https://forums.powershell.org/t/script-powershell/10533 "2018-04-18T06:27:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmwackernie](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@jmwackernie](https://forums.powershell.org/u/jmwackernie)\
**Post date:** [April 18, 2018, 6:27am UTC](https://forums.powershell.org/t/script-powershell/10533/1 "2018-04-18T06:27:19Z")

</div>

Bonjour, jai fait un script powershell et j’aimerais avoir une aide, car le script ne fonctionne pas normalement je vous joins se que je dois faire, et le script que j’ai écrit, mais il ne renomme par mon répertoire.

The Powershell script should do the following:

- Test if directory “\ ecarbssf0007 % username% \ Home \ Documents” exists. If YES, Exit.
- Test if directory “\ ecarbssf0007 % username% \ Home \ My Documents” exists. If YES, Rename directory in “Documents”

This powershell script will be placed upstream of the redirection process of user directories.

and here is my script

$Path = “C:\My Documents”  
$OldText = “My Documents”  
$NewText = “Documents”  
$logFile = ‘c:\temp\test.txt’

$a = Get-Date  
"Lancement du Script de renommage : " + $a \>\> $logfile  
Get-ChildItem $Path -Recurse | %{$_.FullName} |  
Sort-Object -Property Length -Descending |  
% {cd  
Write-Host $_  
$Item = Get-Item $\_  
$PathRoot = $Item.FullName | Split-Path  
$OldName = $Item.FullName | Split-Path -Leaf  
$NewName = $OldName -replace $OldText, $NewText  
$NewPath = $PathRoot | Join-Path -ChildPath $NewName

```
if ($OldName.Contains($OldText)) {
{
  $a = Get-Date
  $erreurInfo = "Impossible de modifier " + $item.Fullname + " "+ $a
  $erreurInfo &gt;&gt; $logFile
}

```

}

}

$a = Get-Date  
"FIN : " + $a \>\> $logfile

---

<div class="post-metadata">

**Author:** ![jmwackernie](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@jmwackernie](https://forums.powershell.org/u/jmwackernie)\
**Post date:** [April 18, 2018, 6:31am UTC](https://forums.powershell.org/t/script-powershell/10533/2 "2018-04-18T06:31:33Z")

</div>

Hello, I made a powershell script and I would like help, because the script does not work normally I join you what I have to do, and the script that I wrote, but it does not rename by my directory.

The Powershell script should do the following:  
– Test if directory “\ ecarbssf0007 % username% \ Home \ Documents” exists. If YES, Exit.  
– Test if directory “\ ecarbssf0007 % username% \ Home \ My Documents” exists. If YES, Rename directory in “Documents”

This powershell script will be placed upstream of the redirection process of user directories.

and here is my script

$Path = “C:\My Documents”  
$OldText = “My Documents”  
$NewText = “Documents”  
$logFile = ‘c:\temp\test.txt’

$a = Get-Date  
"Lancement du Script de renommage : " + $a \>\> $logfile  
Get-ChildItem $Path -Recurse | %{$_.FullName} |  
Sort-Object -Property Length -Descending |  
% {cd  
Write-Host $_  
$Item = Get-Item $\_  
$PathRoot = $Item.FullName | Split-Path  
$OldName = $Item.FullName | Split-Path -Leaf  
$NewName = $OldName -replace $OldText, $NewText  
$NewPath = $PathRoot | Join-Path -ChildPath $NewName

if ($OldName.Contains($OldText)) {  
{  
$a = Get-Date  
$erreurInfo = "Impossible de modifier " + $item.Fullname + " "+ $a  
$erreurInfo \>\> $logFile  
}

}

}

$a = Get-Date  
"FIN : " + $a \>\> $logfile

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [August 26, 2018, 10:21pm UTC](https://forums.powershell.org/t/script-powershell/10533/3 "2018-08-26T22:21:18Z")

</div>

There are many issues in this script. What you would need is pure basics. I would suggest you to start from [here](https://mva.microsoft.com/liveevents/powershell-jumpstart) and a highly recommended book “Learn PowerShell Toolmaking in a Month of Lunches” by Don Jones .

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 9:13pm UTC](https://forums.powershell.org/t/script-powershell/10533/4 "2024-05-16T21:13:24Z")

</div>


