Question Create new folder with copy when not existing

Plus d'informations
il y a 13 ans 4 mois #13066 par Warrer
Aaah great, interesting ! Well too bad I am limited to PS2.0

I apologize but it appears that my request was not correctly formed. To be more precise, I would like to exclude the Public folder and just include the hidden one called Default.

Here is how I do it but it keeps telling me that Path is Null (it works with either exclude or include):

[code:1]
Destination Folder
$Office =\"AppData\Local\Microsoft\Office\"
#File to copy
$OfficeUIFile =\"./olkapptitem.officeUI\"
$users = Get-childitem C:\Users -include Default -exclude Public

$MyScriptParentPath = Split-Path -Path $MyInvocation.mycommand.path -Parent
$users | % {
$Destination = join-path $_.fullname $Office
if (test-path $Destination)
{
Copy-Item (join-path $MyScriptParentPath $OfficeUIFile) -Destination $Destination -Force
}
else{
New-Item $Destination -ItemType Directory -Force
Copy-Item (join-path $MyScriptParentPath $OfficeUIFile) -Destination $Destination -Force
Write-Host \"$Destination did not exist and has been created\"
}

}

[/code:1]

Sorry :/

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 4 mois #13068 par Matthew BETTON
See the help :

[code:1]PS C:\> Get-Help Get-ChildItem -Parameter include

-Include <string[]>
Retrieves only the specified items. The value of this parameter qualifies t
he Path parameter. Enter a path element or pattern, such as \"*.txt\". Wildca
rds are permitted.

The Include parameter is effective only when the command includes the Recur
se parameter or the path leads to the contents of a directory, such as C:\W
indows\*, where the wildcard character specifies the contents of the C:\Win
dows directory.

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false[/code:1]

\"...The Include parameter is effective only when the command includes the Recurse parameter or the path leads to the contents of a directory such as C:\Windows\* ...\"

So it should only work with the exclude parameter.

To be more precise, I would like to exclude the Public folder and just include the hidden one called Default.


I don't understand. Why do you want to list subdirectories in C:\Users while you just want to manage the \"C:\Users\Default\" folder ?

If you want both directories \"Default\" and \"Default User\" :

[code:1]$users = Get-ChildItem c:\users -Force | Where-Object {$_.PsIscontainer -and $_.Name -like \"Default*\"}[/code:1]

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 4 mois #13070 par Warrer
Thanks Matthew,

You get me wrong, I want to copy this file into every not hidden directories in C:\Users (i.e. all users folders) except Public but in addition the Default folder which is hidden.

That's why I wanted to include Default and exclude Public.

Should I have then something like:

$users = Get-childitem C:\Users -include C:\Users\Default Recurse -exclude Public

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 4 mois #13074 par Matthew BETTON
Ok ;)

'-recurse' is not usefull in your case, as it lists all sub folders and files, recursively.

This should do the trick :

[code:1]$users = Get-ChildItem c:\users -Force -Exclude Public,'Default user' | Where-Object {$_.PsIscontainer}[/code:1]

This means : \"All directories, even hidden, but without 'Public' and 'Default user'\".

'Default user' is hidden, but you don't want this one.

This also works :

[code:1]Get-ChildItem c:\users -Force | Where-Object {$_.PsIscontainer -and $_.Name -ne 'Public' -and $_.Name -ne 'Default user'}[/code:1]

You can try it in a console.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 4 mois #13075 par Warrer
Thanks a lot Matthew, it did work !!

I am posting my final code so it can directly help someone with the same needs:

[code:1]
#Destination Folder
$Office =\"AppData\Local\Microsoft\Office\"
#File to copy
$OfficeUIFile =\"./olkapptitem.officeUI\"
$users = Get-ChildItem c:\users -Force -Exclude Public,Administrator,'Default user','All Users','desktop.ini' | Where-Object {$_.PsIscontainer}


$MyScriptParentPath = Split-Path -Path $MyInvocation.mycommand.path -Parent
$users | % {
$Destination = join-path $_.fullname $Office
if (test-path $Destination)
{
Copy-Item (join-path $MyScriptParentPath $OfficeUIFile) -Destination $Destination -Force
}
else{
New-Item $Destination -ItemType Directory -Force
Copy-Item (join-path $MyScriptParentPath $OfficeUIFile) -Destination $Destination -Force
Write-Host \"$Destination did not exist and has been created\"
}

}
[/code:1]

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 4 mois #13077 par Matthew BETTON
Super !

Thank you for your return :)

Could you please tell us what is your country ?

Thanks

Matthew

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.047 secondes
Propulsé par Kunena